# 反转链表 # 输入一个链表,反转链表后,输出新链表的表头。 from tool import ListNode, createLinkList, printList class Solution: # 返回ListNode def ReverseList(self, pHead): # 头插法迭代 newHead = None while pHead: next = pHead.next # 临时节点,暂存当前节点的下一节点 pHead.next = newHead # 原地逆置 newHead = pHead # 新头结点前移 pHead = next # 原头结点后移 return newHead if __name__ == '__main__': s = Solution() ll = createLinkList([1, 2, 3]) printList(s.ReverseList(ll))
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 mergeTwoLists(self, l1, l2): return self.mergeTwoLists2(l1, l2) if __name__ == '__main__': s = Solution() l1 = createLinkList([1,2,4]) l2 = createLinkList([1,3,4]) printList(s.mergeTwoLists(l1, l2))
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 reverseBetween(self, head, m, n): return self.reverseBetween2(head, m, n) if __name__ == '__main__': head = createLinkList("12345") s = Solution() printList(s.reverseBetween(head, 2, 2))
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 mergeKLists(self, lists): return self.mergeKLists3(lists) if __name__ == '__main__': s = Solution() l1 = createLinkList([1, 4, 5]) l2 = createLinkList([1, 3, 4]) l3 = createLinkList([2, 6]) printList(s.mergeKLists([l1, l2, l3]))
class Solution(object): 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 if __name__ == '__main__': head = createLinkList([1,4,3,2,5]) s = Solution() printList(s.partition(head, 3))
if pHead1 != None: tail.next = pHead1 if pHead2 != None: tail.next = pHead2 return dummy.next def Merge2(self, pHead1, pHead2): # 递归 if not pHead1: return pHead2 if not pHead2: return pHead1 dummy = None if pHead1.val < pHead2.val: dummy = pHead1 dummy.next = self.Merge2(pHead1.next, pHead2) else: dummy = pHead2 dummy.next = self.Merge2(pHead1, pHead2.next) return dummy def Merge(self, pHead1, pHead2): return self.Merge2(pHead1, pHead2) if __name__ == '__main__': s = Solution() pHead1 = createLinkList([1, 3, 5]) pHead2 = createLinkList([2, 4, 5]) printList(s.Merge(pHead1, pHead2))
from tool import ListNode, createLinkList, printList class Solution(object): # 头插法迭代 def reverseList1(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return head new_head = None while head: next = head.next # 临时节点,暂存当前节点的下一节点 head.next = new_head # 原地逆置 new_head = head # 新头结点前移 head = next # 原头结点后移 return new_head def reverseList(self, head): return self.reverseList1(head) if __name__ == '__main__': head = createLinkList("12345") s = Solution() printList(s.reverseList(head))
def getIntersectionNode3(self, headA, headB): """ :type head1, head1: ListNode :rtype: ListNode """ pa, pb = headA, headB # 在这里第一轮体现在pa和pb第一次到达尾部会移向另一链表的表头, 而第二轮体现在如果pa或pb相交就返回交点, 不相交最后就是None == None while pa != pb: pa = headB if (pa == None) else pa.next pb = headA if (pb == None) else pb.next return pa def getIntersectionNode(self, headA, headB): return self.getIntersectionNode3(headA, headB) if __name__ == '__main__': headA = createLinkList([4,1]) headB = createLinkList([5,0,1]) headC = createLinkList([8,4,5]) headA.next.next = headC headB.next.next.next = headC s = Solution() printList(s.getIntersectionNode(headA, headB)) # t = set() # t.add(1) # t.add(2) # t.add(2) # print(t) # print(1 in t)