def reverseBetween(self, head, m, n): # write code here dummy = ListNode(0) dummy.next = head count = 0 while head: count += 1 if count == m - 1: front = head print(front.val) if count == m: cur = head print(cur.val) if count == n + 1: behind = head print(behind.val) head = head.next if m == 1: front = dummy print(front.val) if count == n: behind = None prev = behind while cur != behind: temp = cur.next cur.next = prev prev = cur cur = temp front.next = prev return dummy.next
def reverseList(self, head): dummy = ListNode() while head: nxt = head.next head.next = dummy.next dummy.next = head head = nxt return dummy.next
def addTwoNumbers(self, l1, l2): # 84 ms faster than 52.88%, 14.7 MB less than 12.06% s1, s2 = [], [] while l1: s1.append(l1.val) l1 = l1.next while l2: s2.append(l2.val) l2 = l2.next carry = 0 node = None while s1 and s2: carry, value = divmod(s1.pop() + s2.pop() + carry, 10) tail = ListNode(value) tail.next = node node = tail s = s1 or s2 while s: carry, value = divmod(s.pop() + carry, 10) tail = ListNode(value) tail.next = node node = tail if carry: tail = ListNode(carry) tail.next = node node = tail return node
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: dummy = ListNode(0) dummy.next = head first = second = dummy for i in range(n + 1): first = first.next while first: first = first.next second = second.next second.next = second.next.next return dummy.next
def deleteDuplicates(self, head): if not head or not head.next: return head dummy = ListNode(None) dummy.next = head prev = dummy cur = head while cur: while cur.next and cur.next.val == cur.val: cur = cur.next if prev.next == cur: prev = prev.next else: prev.next = cur.next cur = cur.next return dummy.next
def removeNthFromEnd2(self, head: ListNode, n: int) -> ListNode: dummy = ListNode(0) dummy.next = head first, l = head, 0 while first: l += 1 first = first.next first = dummy l -= n for i in range(l): # 不用判断first 题目保证了要删除的结点是有效的 first = first.next # while l > 0: # 这三步和上面的for循环效果一样 # first = first.next # l -= 1 first.next = first.next.next return dummy.next
def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ dummy = ListNode(24) dummy.next = head pre = dummy while True: if head is None or head.next is None: break # swap two nodes need a pre and after after = head.next.next pre.next = head.next pre.next.next = head head.next = after pre = head head = after return dummy.next
def addTwoNumbers2(self, l1, l2): # 80 ms, more stack1, stack2 = [], [] while l1: stack1.append(l1.val) l1 = l1.next while l2: stack2.append(l2.val) l2 = l2.next carry = 0 head = None while stack1 or stack2 or carry: s = (stack1.pop() if stack1 else 0) + (stack2.pop() if stack2 else 0) + carry carry, v = divmod(s, 10) n = ListNode(v) n.next = head head = n return head
def mergeKLists(self, lists: List[ListNode]) -> ListNode: import heapq heap = [] if not lists: return dummy = tail = ListNode(0) for i in range(len(lists)): if lists[i]: heapq.heappush(heap, (lists[i].val, i)) while heap: v, idx = heappop(heap) tail.next = ListNode(v) tail = tail.next # lists[idx]一定是非 NoneType if lists[idx].next: heappush(heap, (lists[idx].next.val, idx)) lists[idx] = lists[idx].next return dummy.next
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: dummy = tail = ListNode(0) while l1 and l2: if l1.val > l2.val: l1, l2 = l2, l1 tail.next = l1 l1 = l1.next tail = tail.next if l1: tail.next = l1 if l2: tail.next = l2 return dummy.next
def partition(self, head, x): # 32ms 98.22% # 先分隔成小大两个链表,然后将两个连接起来 if not head or not head.next: return head small = small_dummy = ListNode(0) big = big_dummy = ListNode(0) while head: if head.val < x: small.next = head small = small.next else: big.next = head big = big.next head = head.next small.next = big_dummy.next # 避免造成环形链表,环形链表不仅不正确,还超时 big.next = None return small_dummy.next
def merge(self, l1, l2): dummy = tail = ListNode(0) while l1 and l2: if l1.val > l2.val: l1, l2 = l2, l1 tail.next = l1 tail = tail.next l1 = l1.next if l1: tail.next = l1 if l2: tail.next = l2 return dummy.next
def mergeTwoLists(self, l1, l2): dummy = l = ListNode() while l1 and l2: if l1.val >= l2.val: l.next = l2 l2 = l2.next else: l.next = l1 l1 = l1.next l = l.next if l1: l.next = l1 if l2: l.next = l2 return dummy.next
if k > 0: heappush(heap, (cnt, Rev(s))) k -= 1 else: heappushpop(heap, (cnt, Rev(s))) ans = [] while heap: cnt, s = heappop(heap) ans.append([s.inner, cnt]) return ans[::-1] if __name__ == '__main__': q = Q() l = ListNode.make_linked_list([1, 2, 3, 4, 5]) print(q.reverseBetween(l, 2, 4)) def maxlenEqualK(arr, k): memo = {0: 0} prefix_sum = 0 ans = 0 for i, n in enumerate(arr, 1): prefix_sum += n if prefix_sum not in memo: memo[prefix_sum] = i counterpart = prefix_sum - k if counterpart in memo: ans = max(i - memo[counterpart], ans) return ans