示例#1
0
 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
     sentinal = ListNode()
     p, carry = sentinal, 0
     while carry or l1 or l2:
         s = carry
         if l1:
             s, l1 = s + l1.val, l1.next
         if l2:
             s, l2 = s + l2.val, l2.next
         p.next = ListNode(s % 10)
         carry = s // 10
         p = p.next
     return sentinal.next
示例#2
0
 def partition(self, head: ListNode, x: int) -> ListNode:
     p1 = h1 = ListNode(-101)
     p2 = h2 = ListNode(101)
     while head:
         if head.val < x:
             p1.next = head
             p1 = p1.next
         else:
             p2.next = head
             p2 = p2.next
         head = head.next
     p1.next, p2.next = h2.next, None
     return h1.next
示例#3
0
 def merge(self, l1: ListNode, l2: ListNode) -> ListNode:
     sentinal = p = ListNode()
     while l1 and l2:
         if l1.val <= l2.val:
             p.next, p, l1 = l1, l1, l1.next
         else:
             p.next, p, l2 = l2, l2, l2.next
     p.next = l1 or l2
     return sentinal.next
示例#4
0
 def removeElements(self, head: ListNode, val: int) -> ListNode:
     sentinel = ListNode(val + 1, head)
     p = sentinel
     while p and p.next:
         if p.next.val == val:
             p.next = p.next.next
         else:
             p = p.next
     return sentinel.next
示例#5
0
 def swapPairs(self, head: ListNode) -> ListNode:
     if not head or not head.next:
         return head
     dummy = ListNode(next=head)
     p, prev = head, dummy
     while p and p.next:
         prev.next = p.next
         q = p.next.next
         p.next.next, p.next = p, q
         prev, p = p, q
     return dummy.next
示例#6
0
 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
     s1, s2 = [], []
     while l1:
         s1.append(l1.val)
         l1 = l1.next
     while l2:
         s2.append(l2.val)
         l2 = l2.next
     dummy, carry = ListNode(), 0
     for e1, e2 in zip_longest(s1[::-1], s2[::-1]):
         s = carry
         if e1:
             s += e1
         if e2:
             s += e2
         carry = s // 10
         dummy.next = ListNode(s % 10, dummy.next)
     if carry == 1:
         dummy.next = ListNode(1, dummy.next)
     return dummy.next
示例#7
0
 def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
     sentinel = ListNode(0)
     p = sentinel
     while l1 and l2:
         if l1.val < l2.val:
             p.next = l1
             l1 = l1.next
         else:
             p.next = l2
             l2 = l2.next
         p = p.next
     p.next = l1 if l1 else l2
     return sentinel.next
示例#8
0
 def insertionSortList(self, head: ListNode) -> ListNode:
     prev = sentinal = ListNode()
     while head:
         nxt = head.next
         # Optimization: search from begining
         # only when head is supposed in front of last insert point
         if head.val <= prev.val:
             prev = sentinal
         while prev.next and prev.next.val < head.val:
             prev = prev.next
         head.next = prev.next
         prev.next = head
         head = nxt
     return sentinal.next
示例#9
0
 def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
     dummy = jump = ListNode(0)
     dummy.next = l = r = head
     while True:
         count = 0
         while r and count < k:  # use r to locate the range
             r, count = r.next, count + 1
         if count == k:
             cur, pre = l, r
             for _ in range(k):
                 cur.next, cur, pre = pre, cur.next, cur
             jump.next, jump, l = pre, l, r  # connect two k-groups
         else:
             return dummy.next
示例#10
0
 def deleteDuplicates(self, head: ListNode) -> ListNode:
     sentinal = ListNode(0, next=head)
     p, was_dup = sentinal, False
     while p and p.next:
         if p.next.next:
             is_dup = p.next.val == p.next.next.val
             if is_dup or was_dup:
                 p.next = p.next.next  # delete p.next but not iterate
             else:
                 p = p.next
             was_dup = is_dup
         else:
             if was_dup:
                 p.next = None  # delete the last duplicate
             p = p.next
     return sentinal.next
示例#11
0
    def reverseBetween(self, head: ListNode, left: int,
                       right: int) -> ListNode:
        if not head:
            return head
        dummy = ListNode(0, head)

        pre, i = dummy, 0
        while i < left - 1:
            i, pre = i + 1, pre.next

        start = pre.next
        then = start.next

        while i < right - 1:
            start.next = then.next
            then.next = pre.next
            pre.next = then
            then = start.next
            i += 1

        return dummy.next