def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if not l1: return l2 if not l2: return l1 if l1.val <= l2.val: l1.next = self.mergeTwoLists(l1.next, l2) return l1 else: l2.next = self.mergeTwoLists(l1, l2.next) return l2
def reverse(self, head: ListNode, tail: ListNode): cur = head new_head = None while head != tail: temp = head.next head.next = new_head new_head = head head = temp head.next = new_head a_head = head a_tail = cur return a_head, a_tail
def partition(self, head: ListNode, x: int) -> ListNode: left_head = ListNode(-1) left = left_head right_head = ListNode(-1) right = right_head while head: if head.val < x: left.next = ListNode(head.val) left = left.next elif head.val >= x: right.next = ListNode(head.val) right = right.next head = head.next left.next = right_head.next return left_head.next
def reverseKGroup(self, head: ListNode, k: int) -> ListNode: if k == 1: return head count = 1 res = cur = ListNode(-1) temp_head_next = None temp = temp_ = head while head: if count == k: count = 1 # 记录当前head结点 temp_head_next = head.next # 交换 after_head, after_tail = self.reverse(temp, head) cur.next = after_head cur = after_tail temp = temp_ = head = temp_head_next continue head = head.next temp_ = temp_.next count += 1 if 1 < count <= k: cur.next = temp_head_next return res.next
def partition(self, head: ListNode, x: int) -> ListNode: left_head = ListNode(-1) right_head = ListNode(-1) left = left_head right = right_head while head: if head.val < x: left.next = head # p1.next并没有对p1操作,因此dummy和p1地址不变。等于head相当于把p1内部存储了东西 left = left.next else: right.next = head right = right.next head = head.next left.next = right_head.next right.next = None return left_head.next
def reverseList(self, head: ListNode) : if not head or not head.next: return head nextNode = self.reverseList(head.next) head.next.next = head head.next = None return nextNode
def lastRemaining(self, n: int, m: int) -> int: if m == 1: return n - 1 head = cur = ListNode(0) for i in range(1, n): cur.next = ListNode(i) cur = cur.next cur.next = head while head != head.next: cur_ = head for i in range(m - 2): cur_ = cur_.next cur_.next = cur_.next.next head = cur_.next return head.val
def reverseList(self, head: ListNode) -> ListNode: new_head = None while head: temp = head.next head.next = new_head new_head = head head = temp return new_head
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: new_head = ListNode(-1) head = new_head while l1 or l2: if l1 and l2: if l1.val < l2.val: head.next = ListNode(l1.val) l1 = l1.next else: head.next = ListNode(l2.val) l2 = l2.next elif l1: head.next = ListNode(l1.val) l1 = l1.next elif l2: head.next = ListNode(l2.val) l2 = l2.next head = head.next return new_head.next
def mergeTwoLists2(self, l1: ListNode, l2: ListNode) -> ListNode: if not l1: while l2: self.cur.next = ListNode(l2.val) self.cur = self.cur.next l2 = l2.next if not l2: while l1: self.cur.next = ListNode(l1.val) self.cur = self.cur.next l1 = l1.next if l1 and l2: if l1.val <= l2.val: self.cur.next = ListNode(l1.val) self.cur = self.cur.next self.mergeTwoLists2(l1.next, l2) else: self.cur.next = ListNode(l2.val) self.cur = self.cur.next self.mergeTwoLists2(l1, l2.next) return self.res.next
def mergeKLists(self, lists): if len(lists) == 0: return elif len(lists) == 1: return lists[0] else: min_heap = [] ans = cur = ListNode(-1) for i in range(len(lists)): if lists[i]: heapq.heappush(min_heap, (lists[i].val, i)) while min_heap: val, index = heapq.heappop(min_heap) cur.next = ListNode(val) cur = cur.next lists[index] = lists[index].next if lists[index]: heapq.heappush(min_heap, (lists[index].val, index)) return ans.next
def reverseList(self, head: ListNode): new_head = None while head: # temp指向当前节点的下一个节点 tmp = head.next # 然后将当前节点指向new_head head.next = new_head # 都前进一位 new_head = head head = tmp return new_head
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: dummy = ListNode(-1) dummy.next = head if m == 1: before_head = head new_head = None for i in range(n - m + 1): temp = head.next head.next = new_head new_head = head head = temp # 结束循环此时head为点4 before_head.next = head return new_head else: # 最开始head是整个链表的head pre_head = None for i in range(m - 1): # 循环的最后一次记录下逆序前的头节点前驱,即点1 if i == m - 2: pre_head = head # 往后移动,找逆序前的头节点 head = head.next # 点2,记录逆置前的头节点 before_head = head # 开始逆序 new_head = None for i in range(n - m + 1): temp = head.next head.next = new_head new_head = head head = temp # 结束循环此时head为点4 # 点2指向点4 before_head.next = head # 点1指向点3 pre_head.next = new_head return dummy.next
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: res = cur = ListNode(-1) ten = 0 one = 0 v = 0 while l1 or l2: va1 = l1.val if l1 else 0 va2 = l2.val if l2 else 0 v = va1 + va2 + ten ten = v // 10 one = v % 10 cur.next = ListNode(one) if l1: l1 = l1.next if l2: l2 = l2.next cur = cur.next if ten != 0: cur.next = ListNode(ten) return res.next
def deleteNode(self, head: ListNode, val: int) -> ListNode: res = cur = ListNode(-1) cur.next = head while head: if head.val != val: head = head.next cur = cur.next else: if head.next is None: cur.next = None break else: cur.next = head.next break return res.next
def swapPairs(self, head: ListNode) -> ListNode: if head.next is None: return head res = cur = ListNode(-1) while head and head.next: # 先在左,后在右 left_right = head # 先在右,后在左 right_left = head.next # 逆转1和2,并且头指针和尾指针弄好 prev = right_left.next # 记录尾指针 left_right.next = prev right_left.next = left_right cur.next = right_left # 记录新的head cur = left_right head = prev return res.next
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: res = cur = ListNode(-1) while l1 or l2: if l1 and l2: v1, v2 = l1.val, l2.val if v1 > v2: cur.next = ListNode(l2.val) cur = cur.next l2 = l2.next continue elif v1 < v2: cur.next = ListNode(l1.val) cur = cur.next l1 = l1.next continue else: cur.next = ListNode(l1.val) cur = cur.next cur.next = ListNode(l2.val) cur = cur.next l1 = l1.next l2 = l2.next continue if l1: cur.next = ListNode(l1.val) cur = cur.next l1 = l1.next continue if l2: cur.next = ListNode(l2.val) cur = cur.next l2 = l2.next continue return res.next
v = 0 while l1 or l2: va1 = l1.val if l1 else 0 va2 = l2.val if l2 else 0 v = va1 + va2 + ten ten = v // 10 one = v % 10 cur.next = ListNode(one) if l1: l1 = l1.next if l2: l2 = l2.next cur = cur.next if ten != 0: cur.next = ListNode(ten) return res.next l1 = ListNode(2) l1.next = ListNode(4) l1.next.next = ListNode(9) l2 = ListNode(5) l2.next = ListNode(6) l2.next.next = ListNode(4) Solution().addTwoNumbers(l1, l2)
返回链表 4->5. """ from tag_listnode import ListNode class Solution: def getKthFromEnd(self, head: ListNode, k: int) -> ListNode: c1 = c2 = head count = 0 while head and head.next: if count < k - 1: head = head.next c2 = c2.next count += 1 else: head = head.next c2 = c2.next c1 = c1.next return c1 l1 = ListNode(2) l1.next = ListNode(4) l1.next.next = ListNode(5) l1.next.next.next = ListNode(7) l1.next.next.next.next = ListNode(9) l1.next.next.next.next.next = ListNode(10) Solution().getKthFromEnd(l1, 3)
def __init__(self): self.res = self.cur = ListNode(-1)
示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 限制: 0 <= 节点个数 <= 5000 """ from tag_listnode import ListNode class Solution: def reverseList(self, head: ListNode) -> ListNode: new_head = None while head: temp = head.next head.next = new_head new_head = head head = temp return new_head l1 = ListNode(1) l1.next = ListNode(2) l1.next.next = ListNode(3) l1.next.next.next = ListNode(4) l1.next.next.next.next = ListNode(5) Solution().reverseList(l1)
continue else: cur.next = ListNode(l1.val) cur = cur.next cur.next = ListNode(l2.val) cur = cur.next l1 = l1.next l2 = l2.next continue if l1: cur.next = ListNode(l1.val) cur = cur.next l1 = l1.next continue if l2: cur.next = ListNode(l2.val) cur = cur.next l2 = l2.next continue return res.next l1 = ListNode(5) l2 = ListNode(1) l2.next = ListNode(2) l2.next.next = ListNode(4) Solution().mergeTwoLists(l1, l2)
head = head.next le += 1 return le # 双指针 class Solution2: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: ha, hb = headA, headB while ha != hb: ha = ha.next if ha else headB hb = hb.next if hb else headA return ha c = ListNode(5) A = ListNode(2) A.next = ListNode(3) A.next.next = ListNode(4) A.next.next.next = c B = ListNode(1) B.next = c B.next.next = ListNode(0) B.next.next.next = ListNode(9) print(Solution2().getIntersectionNode(A, B))
elif len(lists) == 1: return lists[0] else: min_heap = [] ans = cur = ListNode(-1) for i in range(len(lists)): if lists[i]: heapq.heappush(min_heap, (lists[i].val, i)) while min_heap: val, index = heapq.heappop(min_heap) cur.next = ListNode(val) cur = cur.next lists[index] = lists[index].next if lists[index]: heapq.heappush(min_heap, (lists[index].val, index)) return ans.next l1 = ListNode(1) l1.next = ListNode(2) l1.next.next = ListNode(3) l2 = ListNode(1) l2.next = ListNode(3) l2.next.next = ListNode(4) l3 = ListNode(4) l3.next = ListNode(6) l3.next.next = ListNode(7) print(Solution().mergeKLists([l1, l2, l3]))