def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: s1, s2 = [], [] while l1: s1.append(l1.val) l1 = l1.next while l2: s2.append(l2.val) l2 = l2.next # 每次将新节点插入 dummy 后面 # dummy->null dummy = ListNode(-1) carry = 0 while s1 or s2 or carry != 0: a = s1.pop() if s1 else 0 b = s2.pop() if s2 else 0 sum_ = a + b + carry carry = sum_ // 10 val = sum_ % 10 node = ListNode(val) node.next = dummy.next dummy.next = node return dummy.next
def test_is_palindrome(self): node1 = ListNode(1) node2 = ListNode(2) node3 = ListNode(1) node1.next = node2 node2.next = node3 s = Solution() s.isPalindrome(node1)
def test_get_intersection_node(self): s = Solution() node1 = ListNode(1) node2 = ListNode(2) node3 = ListNode(3) node4 = ListNode(4) node5 = ListNode(5) node1.next = node2 node3.next = node2 node5.next = node3 print(s.getIntersectionNode(node1, node5).val)
def reverseKGroup(self, head: ListNode, k: int) -> ListNode: # 洗数据 if not head: return head dummy = ListNode(-1) dummy.next = head cur = dummy while cur: start = cur.next # 将 start, end 置于 k 个节点的开头和末尾 end = cur for _ in range(k): end = end.next if not end: return dummy.next nxt = end.next # 将翻转后 start, end = self.reversList(start, end) cur.next = start end.next = nxt # 进入下一轮 cur = end return dummy.next
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: dummy = ListNode(-1) dummy.next = head slow = fast = dummy for _ in range(n): fast = fast.next for _ in range(m): slow = slow.next # 反转链表模板 prev = fast.next cur = slow while prev != fast: nxt = cur.next cur.next = prev prev = cur cur = nxt # 现在 prev 是反转后,链表的头节点 # 需要将反转之后的链表,接回原链表 cur = dummy for _ in range(m - 1): cur = cur.next cur.next = prev return dummy.next
def test_reverse_list(self): node1 = ListNode(1) node2 = ListNode(2) node1.next = node2 s = Solution() p = s.reverseListRecursive(node1) while p: print(p.val) p = p.next
def get_left_mid(self, head) -> ListNode: # 不接受空节点和单节点 if not head or not head.next: raise ValueError('list must have at least two element') dummy = ListNode(-1) dummy.next = head slow = dummy fast = head while fast and fast.next: slow, fast = slow.next, fast.next.next return slow
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: dummy = ListNode(-1) dummy.next = head fast = dummy # 找到倒数 n + 1 的节点 for _ in range(n + 1): # n 一定有效,所以这里无需判空 fast = fast.next slow = dummy while fast: slow, fast = slow.next, fast.next slow.next = slow.next.next return dummy.next
def reorderList(self, head: ListNode) -> None: """ Do not return anything, modify head in-place instead. """ if not head or not head.next or not head.next.next: return mid = self.get_mid(head) tail = self.reverse_list(mid) while tail != mid: head_next = head.next head.next = tail head = head_next tail_next = tail.next tail.next = head tail = tail_next
def swapPairs(self, head: ListNode) -> ListNode: # 洗数据 if not head or not head.next: return head dummy = ListNode(-1) dummy.next = head cur = dummy while cur.next and cur.next.next: first = cur.next sec = cur.next.next cur.next = sec first.next = sec.next sec.next = first cur = cur.next.next return dummy.next
def deleteNode(self, head: ListNode, val: int) -> ListNode: # 洗数据 if not head: return head # 哑节点,方便删除头节点的情况 # 最后结果返回 dummy.next dummy = ListNode(-1) dummy.next = head prev = dummy cur = head while cur: if cur.val == val: prev.next = prev.next.next break prev, cur = prev.next, cur.next return dummy.next
def deleteDuplicates(self, head: ListNode) -> ListNode: # 洗数据 if not head or not head.next: return head # 可能有头节点变动的情况,要哑节点 dummy = ListNode(-1) dummy.next = head cur = dummy while cur and cur.next and cur.next.next: if cur.next.val == cur.next.next.val: end = cur.next.next while end and end.val == cur.next.val: end = end.next cur.next = end else: cur = cur.next return dummy.next
def deleteNode(self, node: ListNode) -> None: node.val = node.next.val node.next = node.next.next