コード例 #1
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
コード例 #2
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
コード例 #3
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
コード例 #4
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
コード例 #5
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
コード例 #6
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
コード例 #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 reverseBetween(self, head: ListNode, left: int,
                       right: int) -> ListNode:
        #遍历了两次
        if not head:
            return
        dummy_head = ListNode(-1)
        pre = dummy_head
        pre.next = head
        start = head
        for _ in range(left - 1):
            start = start.next
            pre = pre.next

        tail = head
        for _ in range(right):
            tail = tail.next

        def reverse(s, tail):
            if not s:
                return None
            ret = tail
            cur = s
            while cur and cur != tail:
                cur.next, cur, ret = ret, cur.next, cur
            return ret

        r = reverse(start, tail)
        pre.next = r
        return dummy_head.next
コード例 #9
0
    def partition(self, head: ListNode, x: int) -> ListNode:
        if not head:return
        s_dummy_head,l_dummy_head = ListNode(-1),ListNode(-1)
        small_foot, large_foot = s_dummy_head, l_dummy_head
        while head:
            if head.value < x:
                small_foot.next = head
                small_foot = small_foot.next
            else:
                large_foot.next = head
                large_foot = large_foot.next
            head = head.next

        large_foot.next = None
        small_foot.next = l_dummy_head.next
        return s_dummy_head.next
コード例 #10
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
コード例 #11
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
コード例 #12
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
コード例 #13
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
コード例 #14
0
 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
コード例 #15
0
 def merge(self, l1, l2):
     dummy_head = ListNode(-1)
     cur = dummy_head
     while l1 and l2:
         if l1.value < l2.value:
             cur.next = l1
             l1 = l1.next
         else:
             cur.next = l2
             l2 = l2.next
         cur = cur.next
     cur.next = l1 if l1 else l2
     return dummy_head.next
コード例 #16
0
 def mergeTwoLists1(self, l1: ListNode, l2: ListNode) -> ListNode:
     dummy_head = ListNode(-1)
     l = dummy_head
     #时间复杂度O(n+m) 空间复杂度O(1)
     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
コード例 #17
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
コード例 #18
0
 def mergeKLists3(self, lists: List[ListNode]) -> ListNode:
     # 时间复杂度O(kn * logk) 空间复杂度O(k)
     if not lists: return None
     import heapq
     import queue
     q = []
     for l in lists:
         if l:
             heapq.heappush(q, E(l.value, l))
     dummy_head = ListNode(-1)
     tail = dummy_head
     while q:
         e = heapq.heappop(q)
         tail.next, tail = e.node, e.node
         if e.node.next:
             heapq.heappush(q, E(e.node.next.value, e.node.next))
     return dummy_head.next
コード例 #19
0
 def copyRandomList3(self, head):
     if not head: return
     #时间复杂度O(n) 空间复杂度O(1)
     cur = head
     while cur:
         node = ListNode(cur.value, True)
         cur.next, node.next, cur = node, cur.next, cur.next
     cur = head
     while cur:
         cur.next.random = cur.random.next if cur.random else None
         cur = cur.next.next
     old_head, new_head, ret = head, head.next, head.next
     while old_head:
         old_head.next = old_head.next.next if old_head.next else None
         new_head.next = new_head.next.next if new_head.next else None
         new_head = new_head.next
         old_head = old_head.next
     return ret
コード例 #20
0
    def copyRandomList2(self, head):
        if not head: return

        #时间复杂度O(n) 空间复杂度O(n)
        def clone_node(node):
            if not node: return
            if node not in visited:
                visited[node] = ListNode(node.value, True)
            return visited[node]

        visited = {}
        old_head, new_head = head, ListNode(head.value, True)
        visited[old_head] = new_head
        while old_head:

            new_head.next = clone_node(old_head.next)
            new_head.random = clone_node(old_head.random)

            new_head = new_head.next
            old_head = old_head.next
        return visited[head]
コード例 #21
0
 def clone_node(node):
     if not node: return
     if node not in visited:
         visited[node] = ListNode(node.value, True)
     return visited[node]