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 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 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 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
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)
示例: 输入: 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)
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]))
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)
right.next = head right = right.next head = head.next left.next = right_head.next right.next = None return left_head.next class Solution1: 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 l1 = ListNode(1) l1.next = ListNode(5) l1.next.next = ListNode(3) l1.next.next.next = ListNode(2) l2 = Solution1().partition(l1, 3)