def reverseBetween2(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ if not head: return head # 哑结点 dummy = ListNode(None) dummy.next = head preNodeM = dummy for i in range(m - 1): preNodeM = preNodeM.next head = preNodeM.next new_head = None new_tail = head for i in range(n - m + 1): next = head.next head.next = new_head new_head = head head = next preNodeM.next = new_head # 将逆置段的前一个节点 与 逆转后的链表头部相连 new_tail.next = head # 将逆转后的链表尾部 与 逆置段的后一个节点相连 return dummy.next
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: placeholder = 0 cur_1 = l1 cur_2 = l2 # max_iters = max(len(l1), len(l2)) + 10 head = ListNode() cur = head while True: sum_ = placeholder if cur_1: sum_ += cur_1.val if cur_2: sum_ += cur_2.val # ----------------------------- result_digit = sum_ % 10 placeholder = sum_ // 10 cur.val = result_digit # move to next node if cur_1 is not None: cur_1 = cur_1.next if cur_2 is not None: cur_2 = cur_2.next if cur_1 or cur_2 or placeholder != 0: next_node = ListNode() cur.next = next_node cur = next_node else: break return head
def partition(self, head, x): """ :type head: ListNode :type x: int :rtype: ListNode """ lesshead = ListNode(None) lessptr = lesshead morehead = ListNode(None) moreptr = morehead while head: if head.val < x: lessptr.next = head lessptr = lessptr.next else: moreptr.next = head moreptr = moreptr.next head = head.next moreptr.next = None # 将链表尾置零,否则若最后一个是比x小的,会导致成为带环链表 lessptr.next = morehead.next return lesshead.next
def Merge1(self, pHead1, pHead2): # 循环 dummy = ListNode(None) tail = dummy while pHead1 and pHead2: if pHead1.val < pHead2.val: tail.next = pHead1 tail = tail.next pHead1 = pHead1.next else: tail.next = pHead2 tail = tail.next pHead2 = pHead2.next if pHead1 != None: tail.next = pHead1 if pHead2 != None: tail.next = pHead2 return dummy.next
def mergeTwoLists2(self, l1, l2): """ 迭代 时间复杂度:O(m+n),空间复杂度:O(1) """ newHead = ListNode(None) cur = newHead while l1 and l2: if l1.val < l2.val: cur.next = l1 cur = cur.next l1 = l1.next else: cur.next = l2 cur = cur.next l2 = l2.next if l1: cur.next = l1 if l2: cur.next = l2 return newHead.next
def mergeKLists3(self, lists): import heapq small = [] # 建立头结点最小堆 x = 0 for i in lists: if i: heapq.heappush( small, (i.val, x, i) ) # 比较tuple成员,若第一项相同,则会比较下一项。而ListNode没有比较方法,py3就会抛出异常。添加x,使比较继续。 x += 1 # 构建新有序链表 head = ptr = ListNode(None) while small: val, x, idx = heapq.heappop(small) ptr.next = idx ptr = ptr.next idx = idx.next if idx: heapq.heappush(small, (idx.val, x, idx)) return head.next
def mergeKLists2(self, lists): from queue import PriorityQueue que = PriorityQueue() # 建立头结点优先队列 x = 0 for i in lists: if i: que.put( (i.val, x, i) ) # 比较tuple成员,若第一项相同,则会比较下一项。而ListNode没有比较方法,py3就会抛出异常。添加x,使比较继续。 x += 1 # 构建新有序链表 head = ptr = ListNode(None) while not que.empty(): val, x, idx = que.get() ptr.next = idx ptr = ptr.next idx = idx.next if idx: que.put((idx.val, x, idx)) return head.next
return False # 快慢指针 # 时间复杂度:O(n),空间复杂度:O(1) def hasCycle2(self, head): """ :type head: ListNode :rtype: bool """ if head == None or head.next == None: return False fast, slow = head, head while fast: slow = slow.next fast = fast.next if fast == None: return False fast = fast.next if slow == fast: return True return False def hasCycle(self, head): return self.hasCycle2(head) if __name__ == '__main__': head = createLinkList([i for i in range(8)]) # head.next.next.next.next.next.next.next = head.next.next s = Solution() # print(s.hasCycle(head)) print(s.hasCycle(ListNode(None)))