예제 #1
0
 def addTwoNumbers(self, l1, l2):
     """
     :type l1: ListNode
     :type l2: ListNode
     :rtype: ListNode
     """
     bonus = 0
     c = l1
     len1 = 0
     while c:
         len1 += 1
         c = c.next
     c = l2
     len2 = 0
     while c:
         len2 += 1
         c = c.next
     if len1 < len2:
         l1, l2 = l2, l1
     prettyPrintLinkedList(l1)
     prettyPrintLinkedList(l2)
     res = l1
     while l2:
         bonus, l1.val = divmod(l1.val+l2.val+bonus, 10)
         last = l1
         l1 = l1.next
         l2 = l2.next
     while bonus and l1:
         bonus, l1.val = divmod(l1.val+bonus, 10)
         last = l1
         l1 = l1.next
     if bonus:
         last.next = ListNode(1)
     return res
예제 #2
0
    def splitListToParts(self, root, k):
        """
        :type root: ListNode
        :type k: int
        :rtype: List[ListNode]
        """
        length = 0
        current = root
        while current:
            length += 1
            current = current.next
        single_length, rest = divmod(length, k)
        print(single_length, rest)
        res = []

        current = root
        j = 0
        while j < k:
            i = 1
            res.append(current)
            while i < single_length:
                current = current.next
                i += 1
            if rest and single_length >= 1:
                current = current.next
                rest -= 1
            if current:
                temp = current.next
                current.next = None
                current = temp
            prettyPrintLinkedList(res[j])
            j += 1
        return res
예제 #3
0
        len1 = 0
        while c:
            len1 += 1
            c = c.next
        c = l2
        len2 = 0
        while c:
            len2 += 1
            c = c.next
        if len1 < len2:
            l1, l2 = l2, l1
        prettyPrintLinkedList(l1)
        prettyPrintLinkedList(l2)
        res = l1
        while l2:
            bonus, l1.val = divmod(l1.val+l2.val+bonus, 10)
            last = l1
            l1 = l1.next
            l2 = l2.next
        while bonus and l1:
            bonus, l1.val = divmod(l1.val+bonus, 10)
            last = l1
            l1 = l1.next
        if bonus:
            last.next = ListNode(1)
        return res

a = stringToListNode("[5]")
b = stringToListNode("[5]")
prettyPrintLinkedList(Solution().addTwoNumbers(a,b))
        
예제 #4
0
from linkedtools import stringToListNode, prettyPrintLinkedList
class Solution:
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        length = 0
        node = head
        while node:
            node = node.next
            length += 1
        target = length - n
        if target == 0:
            return head.next
        else:
            node = head
            for i in range(target-1):
                node = node.next
            node.next = node.next.next
            return head

a = stringToListNode("[1,2,3,4,5]")
prettyPrintLinkedList(Solution().removeNthFromEnd(a, 5))
예제 #5
0
        :type head: ListNode
        :type m: int
        :type n: int
        :rtype: ListNode
        """
        if head is None or head.next is None or m == n:
            return head
        headnode = ListNode(-1)
        headnode.next = head
        anchor = headnode
        for _ in range(m - 1):
            anchor = anchor.next
        a = anchor.next
        b = a.next
        c = b.next

        k = n - m
        for _ in range(k):
            b.next = a
            if c:
                a, b, c = b, c, c.next
            else:
                a, b = b, c
        anchor.next.next = b
        anchor.next = a
        return headnode.next


a = stringToListNode("[1,2,3]")
prettyPrintLinkedList(Solution().reverseBetween(a, 2, 3))
예제 #6
0
from linkedtools import ListNode, prettyPrintLinkedList, stringToListNode


class Solution:
    def oddEvenList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head
        head1 = head
        head2 = head.next
        c1 = head1
        c2 = head2
        while c2 and c2.next:
            c1.next = c2.next
            c1 = c1.next
            c2.next = c1.next
            c2 = c2.next
        c1.next = head2
        return head1


a = stringToListNode("[]")
prettyPrintLinkedList(Solution().oddEvenList(a))
예제 #7
0

class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        headnode = ListNode(None)
        headnode.next = head
        delete = False
        p = headnode  # prev
        c = head  # current
        while c:
            while c.next and c.val == c.next.val:
                c.next = c.next.next
                if not delete:
                    delete = True
            if delete:
                c = c.next
                p.next = c
                delete = False
            else:
                c = c.next
                p = p.next
        return headnode.next


a = stringToListNode("[1,1,1,2,2,4]")
prettyPrintLinkedList(Solution().deleteDuplicates(a))
예제 #8
0
from linkedtools import ListNode, prettyPrintLinkedList, stringToListNode


class Solution:
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        c = head
        while True:
            if c and c.next:
                c.val, c.next.val = c.next.val, c.val
                c = c.next.next
            else:
                break
        return head


a = stringToListNode("[]")
prettyPrintLinkedList(Solution().swapPairs(a))
예제 #9
0
        if head is None or head.next is None:
            return head
        sorted = head
        unsorted = head.next
        inf = ListNode(float("inf"))
        sorted.next = inf
        # sorted.next.next = sorted

        while unsorted:
            c = sorted
            while c.val < unsorted.val:
                c = c.next
            temp = unsorted
            unsorted = unsorted.next

            temp.next = c.next
            c.next = temp
            c.val, temp.val = temp.val, c.val

        while True:
            if c.next.val == float("inf"):
                break
            c = c.next
        c.next = None
        return sorted


a = stringToListNode("[6,5,3,1,8,7,2,4]")
# a = stringToListNode("[1,6]")
prettyPrintLinkedList(Solution().insertionSortList(a))
예제 #10
0
 def show(self):
     prettyPrintLinkedList(self.headnode)