def deleteDuplicates(self, head: ListNode) -> ListNode: """ 84ms 31.42% 12.9MB 99.67% fast and slow pointer :param self: :param head: :return: """ if not head or not head.next: return head dummy = ListNode(0) dummy.next = head slow = dummy fast = dummy.next while fast: if fast.next and fast.next.val == fast.val: tmp = fast.val while fast and tmp == fast.val: fast = fast.next else: slow.next = fast slow = fast fast = fast.next slow.next = fast return dummy.next
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: """ 52ms 93.76% 13.1MB 83.1% :param self: :param head: :param n: :return: """ if not head: return head dummy = ListNode(0) dummy.next = head fast = dummy while n: fast = fast.next n -= 1 slow = dummy while fast and fast.next: fast = fast.next slow = slow.next slow.next = slow.next.next return dummy.next
def reverseKGroup(self, head: ListNode, k: int) -> ListNode: """ 尾插法 :param self: :param head: :param k: :return: """ dummy = ListNode(0) dummy.next = head pre = dummy tail = dummy while True: count = k while count and tail: count -= 1 tail = tail.next if not tail: break head = pre.next while pre.next != tail: cur = pre.next pre.next = cur.next cur.next = tail.next tail.next = cur pre = head tail = head return dummy.next
def partition(head: ListNode, x: int) -> ListNode: """ 68ms 34.59% 12.9MB 98.15% :param head: :param x: :return: """ before = before_head = ListNode(0) after = after_head = ListNode(0) while head: # If the original list node is lesser than the given x, # assign it to the before list. if head.val < x: before.next = head before = before.next else: # If the original list node is greater or equal to the given x, # assign it to the after list. after.next = head after = after.next # move ahead in the original list head = head.next # Last node of "after" list would also be ending node of the reformed list after.next = None # Once all the nodes are correctly assigned to the two lists, # combine them to form a single list which would be returned. before.next = after_head.next return before_head.next
def swapPairs(self, head: ListNode) -> ListNode: """ 88ms 7.43% 13MB 89.91% 和32秒那个最快的思路是一样的...... :param self: :param head: :return: """ cur = ListNode(0) cur.next = head head = cur while cur and cur.next and cur.next.next: temp = cur.next.next cur.next.next = temp.next temp.next = cur.next cur.next = temp cur = cur.next.next return head.next
def swapPairs2(self, head: ListNode) -> ListNode: """ 24ms """ if not head or not head.next: return head tmp = head.next head.next = self.swapPairs(head.next.next) tmp.next = head return tmp
def deleteDuplicates2(self, head: ListNode) -> ListNode: """ recursive :param self: :param head: :return: """ if not head: return head if head.next and head.val == head.next.val: while head.next and head.val == head.next.val: head = head.next return self.deleteDuplicates(head.next) else: head.next = self.deleteDuplicates(head.next) return head
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: """ 92ms 23.08% 13.3MB 17.61% :param self: :param l1: :param l2: :return: """ cur = res = ListNode(0) while l1 or l2: if l1 is not None and (l2 is None or l1.val <= l2.val): cur.next = l1 l1 = l1.next else: cur.next = l2 l2 = l2.next cur = cur.next return res.next
def reverseKGroup(self, head: ListNode, k: int) -> ListNode: """ Stack 112ms 21.14% 13.9MB 99.63% :param self: :param head: :param k: :return: """ dummy = ListNode(0) p = dummy while True: count = k stack = [] tmp = head while count and tmp: stack.append(tmp) tmp = tmp.next count -= 1 # 注意,目前tmp所在k+1位置 # 说明剩下的链表不够k个,跳出循环 if count: p.next = head break # 翻转操作 while stack: p.next = stack.pop() p = p.next # 与剩下链表连接起来 p.next = tmp head = tmp return dummy.next
def mergeTwoList(self, l1: ListNode, l2: ListNode) -> ListNode: """ 84ms 28.26% 12.8MB 99.29% :param l2: :param l1: :param self: """ prehead = ListNode(-1) prev = prehead while l1 and l2: if l1.val <= l2.val: prev.next = l1 l1 = l1.next else: prev.next = l2 l2 = l2.next prev = prev.next prev.next = l1 if l1 is not None else l2 return prehead.next
def reverseKGroup(self, head: ListNode, k: int) -> ListNode: """ 递归 :param self: :param head: :param k: :return: """ cur = head count = 0 while cur and count!= k: cur = cur.next count += 1 if count == k: cur = self.reverseKGroup(cur, k) while count: tmp = head.next head.next = cur cur = head head = tmp count -= 1 head = cur return head