コード例 #1
0
 def mergeTwoLists2(self, l1: ListNode, l2: ListNode) -> ListNode:  #递归
     #时间复杂度O(n+m) 空间复杂度O(n+m)
     if not l1:
         return l2
     elif not l2:
         return l1
     elif l1.value <= l2.value:
         l1.next = self.mergeTwoLists2(l1.next, l2)
         return l1
     elif l2.value < l1.value:
         l2.next = self.mergeTwoLists2(l1, l2.next)
         return l2
コード例 #2
0
 def reverseList1(self, head: ListNode) -> ListNode:  # 递归
     if not head or not head.next:
         return head
     p = self.reverseList1(head.next)
     head.next.next = head
     head.next = None
     return p
コード例 #3
0
    def sortList2(self, head: ListNode) -> ListNode:
        if not head:
            return
        # 时间复杂度O(n logn) 空间复杂度O(1)
        def merge(l1, l2):
            dummy_head = ListNode(-1)
            l = dummy_head
            while l1 and l2:
                if l1.value <= l2.value:
                    l.next = l1
                    l1 = l1.next
                else:
                    l.next = l2
                    l2 = l2.next
                l = l.next
            l.next = l2 if l2 else l1
            return dummy_head.next
        length = 0
        node = head
        while node:
            length += 1
            node = node.next

        dummyHead = ListNode(-1)
        dummyHead.next =head
        subLength = 1
        while subLength < length:
            prev, curr = dummyHead, dummyHead.next
            while curr:
                head1 = curr
                for i in range(1, subLength):
                    if curr.next:
                        curr = curr.next
                    else:
                        break
                head2 = curr.next
                curr.next = None
                curr = head2
                for i in range(1, subLength):
                    if curr and curr.next:
                        curr = curr.next
                    else:
                        break

                succ = None
                if curr:
                    succ = curr.next
                    curr.next = None

                merged = merge(head1, head2)
                prev.next = merged
                while prev.next:
                    prev = prev.next
                curr = succ
            subLength <<= 1

        return dummyHead.next
コード例 #4
0
 def recur(node):
     if not node:
         return
     if node in visited:
         return visited[node]
     new_node = ListNode(node.value, True)
     visited[node] = new_node
     new_node.next = recur(node.next)
     new_node.random = recur(node.random)
     return new_node
コード例 #5
0
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        dummy_head = ListNode(-1)
        dummy_head.next = head
        third_place = dummy_head
        first_place, second_place = head, head
        for _ in range(n):
            first_place = first_place.next

        while first_place:
            first_place, second_place, third_place = first_place.next, second_place.next, third_place.next
        third_place.next = second_place.next
        return dummy_head.next
コード例 #6
0
 def reverseBetween2(self, head: ListNode, left: int,
                     right: int) -> ListNode:
     dummy_head = ListNode(-1)
     dummy_head.next = head
     pre = dummy_head
     for _ in range(left - 1):
         pre = pre.next
     cur = pre.next
     for _ in range(right - left):
         next = cur.next
         cur.next, next.next, pre.next = next.next, pre.next, next
     return dummy_head.next
コード例 #7
0
 def isPalindrome2(self, head: ListNode) -> bool:
     if not head: return
     dummy_head = ListNode(-1)
     cur = head
     dummy_head.next = head
     pre = dummy_head
     while cur:
         cur.pre = pre
         cur, pre = cur.next, pre.next
     foot = pre
     while head:
         if head.val != foot.val:
             return False
         head, foot = head.next, foot.pre
     return True
コード例 #8
0
    def swapPairs0(self, head: ListNode) -> ListNode:  # 迭代
        dummyHead = ListNode(-1)
        dummyHead.next = head
        cur_node = dummyHead
        while cur_node.next and cur_node.next.next:
            node1 = cur_node.next
            node2 = cur_node.next.next

            # cur_node.next = node2
            # node1.next = node2.next
            # node2.next = node1
            cur_node.next, node1.next, node2.next = node2, node2.next, node1

            cur_node = node1
        return dummyHead.next
コード例 #9
0
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        dummy_head = ListNode(-1)
        dummy_head.next = head
        pre, cur = dummy_head, head
        #时间复杂度O(n) 空间复杂度O(1)
        while cur and cur.next:

            if cur.value == cur.next.value:
                while cur.next and cur.value == cur.next.value:
                    cur.next = cur.next.next
                pre.next = cur.next
            else:
                pre = pre.next
            cur = cur.next

        return dummy_head.next
コード例 #10
0
 def insertionSortList(self, head: ListNode) -> ListNode:
     #时间复杂度O(n*n)
     if not head: return None
     dummy_head = ListNode(-1)
     dummy_head.next = head
     last_sorted = head
     cur = head.next
     while cur:
         if last_sorted.value > cur.value:
             pre = dummy_head
             while pre.next.value <= cur.value:
                 pre = pre.next
             pre.next, cur.next, last_sorted.next = cur, pre.next, cur.next
         else:
             last_sorted = last_sorted.next
         cur = last_sorted.next
     return dummy_head.next
コード例 #11
0
 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
     stack1 = []
     stack2 = []
     while l1:
         stack1.append(l1.value)
         l1 = l1.next
     while l2:
         stack2.append(l2.value)
         l2 = l2.next
     #时间复杂度O(max(m,n)) 空间复杂度O(n+m)
     ret = None
     add = 0
     while stack1 or stack2 or add != 0:
         x = stack1.pop() if stack1 else 0
         y = stack2.pop() if stack2 else 0
         sum_ret = x + y + add
         add = sum_ret // 10
         node = ListNode(sum_ret % 10)
         node.next, ret = ret, node
     return ret
コード例 #12
0
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        # 翻转一个子链表,并且返回新的头与尾
        def reverse(head, tail):
            if not head: return
            cur = head
            new_head = tail
            while cur != tail:
                cur.next, cur, new_head = new_head, cur.next, cur
            return new_head, head

        dummy_head = ListNode(-1)
        dummy_head.next = head
        hair = dummy_head
        while head:
            foot = hair
            for _ in range(k):
                foot = foot.next
                if not foot:
                    return dummy_head.next
            tail = foot.next
            head, foot = reverse(hair.next, tail)
            hair.next, hair = head, foot
        return dummy_head.next