Esempio n. 1
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        dummyHead = ListNode(-1)
        cur = dummyHead
        x = 0
        while l1 or l2:
            sumTmp = None
            if l1 and l2:
                sumTmp = l1.val + l2.val + x
                l1, l2 = l1.next, l2.next
            elif l1 is None:
                sumTmp = l2.val + x
                l2 = l2.next
            else:
                sumTmp = l1.val + x
                l1 = l1.next

            if sumTmp >= 10:
                x = 1
                sumTmp = sumTmp % 10
            else:
                x = 0
            node = ListNode(sumTmp)
            cur.next = node
            cur = cur.next
        if x == 1:
            node = ListNode(1)
            cur.next = node
        return dummyHead.next
Esempio n. 2
0
 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
     dummyHead = ListNode(-1)
     stack1 = list()
     stack2 = list()
     while l1:
         stack1.append(l1.val)
         l1 = l1.next
     while l2:
         stack2.append(l2.val)
         l2 = l2.next
     x = 0
     while stack1 or stack2:
         sumTmp = None
         if stack1 and stack2:
             sumTmp = stack1.pop() + stack2.pop() + x
         elif stack2:
             sumTmp = stack2.pop() + x
         else:
             sumTmp = stack1.pop() + x
         if sumTmp >= 10:
             sumTmp = sumTmp % 10
             x = 1
         else:
             x = 0
         node = ListNode(sumTmp)
         node.next = dummyHead.next
         dummyHead.next = node
     if x == 1:
         node = ListNode(1)
         node.next = dummyHead.next
         dummyHead.next = node
     return dummyHead.next
Esempio n. 3
0
 def removeElements(self, head: ListNode, val: int) -> ListNode:
     dummyHead = ListNode(-1)
     dummyHead.next = head
     pre, cur = dummyHead, head
     while cur:
         if cur.val == val:
             pre.next = cur.next
         else:
             pre = cur
         cur = cur.next
     return dummyHead.next
Esempio n. 4
0
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        dummyHead = ListNode(-1)
        dummyHead.next = head
        pre = dummyHead
        for i in range(1, k):
            pre = pre.next
        cur = pre.next
        next = cur.next
        pre.next = next

        dummyHead.next = cur
        cur.next = head
        return dummyHead.next
Esempio n. 5
0
 def mergeKLists(self, lists: List[ListNode]) -> ListNode:
     pq = queue.PriorityQueue()
     for link in lists:
         while link:
             pq.put(link.val)
             link = link.next
     dummyHead = ListNode(-1)
     cur = dummyHead
     while not pq.empty():
         node = ListNode(pq.get())
         cur.next = node
         cur = cur.next
     return dummyHead.next
Esempio n. 6
0
 def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
     dummyHead = ListNode(-1)
     dummyHead.next = head
     p = dummyHead  #要删除节点的前一个索引位置
     q = dummyHead
     for _ in range(n + 1):
         q = q.next
     while q:
         p = p.next
         q = q.next
     delNode = p.next
     nextNode = delNode.next
     p.next = nextNode
     return dummyHead.next
Esempio n. 7
0
 def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
     dummyHead = ListNode(-1)
     dummyHead.next = head
     pre = dummyHead
     while pre:
         test = pre
         for i in range(n + 1):
             test = test.next
         if test is None:
             break
         pre = pre.next
     delNode = pre.next
     nextNode = delNode.next
     pre.next = nextNode
     return dummyHead.next
Esempio n. 8
0
 def partition(self, head: ListNode, x: int) -> ListNode:
     lDummyHead = ListNode(-1)
     rDummyHead = ListNode(-1)
     lCur = lDummyHead
     rCur = rDummyHead
     while head:
         val = head.val
         node = ListNode(val)
         if val < x:
             lCur.next = node
             lCur = lCur.next
         else:
             rCur.next = node
             rCur = rCur.next
         head = head.next
     lCur.next = rDummyHead.next
     return lDummyHead.next
Esempio n. 9
0
 def oddEvenList(self, head: ListNode) -> ListNode:
     lDummyHead = ListNode(-1)
     rDummyHead = ListNode(-1)
     lCur = lDummyHead
     rCur = rDummyHead
     i = 1
     while head:
         node = ListNode(head.val)
         if i % 2 == 1:
             lCur.next = node
             lCur = lCur.next
         else:
             rCur.next = node
             rCur = rCur.next
         head = head.next
         i += 1
     lCur.next = rDummyHead.next
     return lDummyHead.next
Esempio n. 10
0
 def rotateRight(self, head: ListNode, k: int) -> ListNode:
     if not head or not head.next:
         return head
     dummyHead = ListNode(-1)
     dummyHead.next = head
     countCur = dummyHead.next
     count = 0
     while countCur:
         countCur = countCur.next
         count += 1
     for _ in range(k % count):  # 解决非必要的旋转
         p = dummyHead
         q = dummyHead.next
         while q.next:
             p = p.next  # 指向倒数第2个节点
             q = q.next  # 指向最后一个node
         q.next = dummyHead.next
         dummyHead.next = q
         p.next = None
     return dummyHead.next
Esempio n. 11
0
    def reverseList(self, head: ListNode) -> ListNode:
        dummyHead = ListNode(-1)
        while head:
            node = ListNode(head.val)
            node.next = dummyHead.next
            dummyHead.next = node

            head = head.next
        return dummyHead.next