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_insertion_sort_list(self): s = Solution() ListNode() self.assertEqual( '1->2->3->4', s.insertionSortList(build_list('4->2->1->3')).entire_list()) self.assertEqual( '-1->0->3->4->5', s.insertionSortList(build_list('-1->5->3->4->0')).entire_list())
def test_delete_duplicates(self): s = Solution() ListNode() self.assertEqual( '1->2', s.deleteDuplicates(build_list('1->1->2')).entire_list()) self.assertEqual( '1->2->3', s.deleteDuplicates(build_list('1->1->2->3->3')).entire_list())
def partition_dummy(self, head: ListNode, x: int) -> ListNode: if not head: return head lth = ltt = ListNode() geh = get = ListNode() while head: if head.val < x: ltt.next = head ltt = ltt.next elif head.val >= x: get.next = head get = get.next head = head.next get.next = None ltt.next = geh.next return lth.next
def oddEvenList(self, head: ListNode) -> ListNode: if not head: return head count = 1 eh = et = ListNode() oh = ot = ListNode() while head: if count % 2 == 0: et.next = head et = et.next else: ot.next = head ot = ot.next head = head.next count += 1 et.next = None ot.next = eh.next return oh.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 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 mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: # 洗数据 if not l1 or not l2: return l1 or l2 dummy = ListNode(-1) cur = dummy while l1 and l2: if l1.val <= l2.val: cur.next = l1 l1 = l1.next else: cur.next = l2 l2 = l2.next cur = cur.next cur.next = l1 or l2 return dummy.next
def insertionSortList(self, head: ListNode) -> ListNode: if not head or not head.next: return head dummy = ListNode(-1) prev = dummy cur = head while cur: # 第一次进行插入排序,或者下一个节点应该插在前面 if not prev.next or prev.next.val >= cur.val: prev = dummy # <= 为了稳定排序,用 3->3->3 里例子看一下 while prev.next and prev.next.val <= cur.val: prev = prev.next # 找到插入位置,插入新节点 cur.next, prev.next, cur = prev.next, cur, cur.next # 将 prev 放到开头 # prev = dummy return dummy.next
def test_swap_pairs(self): s = Solution() ListNode() self.assertEqual('2->1->4->3', s.swapPairs(build_list("1->2->3->4")).entire_list())
def test_entire_list(self): node = ListNode(1, ListNode(2, ListNode(3))) self.assertEqual("1->2->3", entire_list(node))