result = swap(head) return result # 迭代速度更快 class NewSolution: def swapPairs(self, head: ListNode) -> ListNode: dummy = ListNode(-1) prev_node = dummy while head and head.next: first = head second = head.next prev_node.next = second first.next = second.next second.next = first prev_node = first head = first.next prev_node.next = head return dummy.next if __name__ == '__main__': from problems.utils import create_list, print_list print_list(NewSolution().swapPairs(create_list([1, 2, 3, 4]))) print_list(NewSolution().swapPairs(create_list([1, 2, 3]))) print_list(NewSolution().swapPairs(create_list([1])))
tail = pre for i in range(k): tail = tail.next if not tail: return dummy.next next_ = tail.next head, tail = self.reverse(head, tail) pre.next = head tail.next = next_ pre = tail head = tail.next return dummy.next def reverse(self, head: ListNode, tail: ListNode): prev = tail.next p = head while prev != tail: next_ = p.next p.next = prev prev = p p = next_ return tail, head if __name__ == '__main__': from problems.utils import create_list, print_list print_list(Solution().reverseKGroup(create_list([1, 2]), 2))
self.val = x self.next = None class Solution: def getKthFromEnd(self, head: ListNode, k: int) -> ListNode: if not head or k < 1: return None slow = fast = head for i in range(k): if not fast: return None fast = fast.next while fast: slow = slow.next fast = fast.next return slow if __name__ == '__main__': from problems.utils import create_list, print_list print_list(Solution().getKthFromEnd(create_list([1]), 1)) print_list(Solution().getKthFromEnd(create_list([1]), 0)) print_list(Solution().getKthFromEnd(create_list([]), 1)) print_list(Solution().getKthFromEnd(create_list([1, 2, 3, 4, 5]), 2)) print_list(Solution().getKthFromEnd(create_list([1, 2, 3, 4, 5]), 5))
tail = prev_node.next for _ in range(k): if not tail: return dummy.next tail = tail.next _next = tail new_head, tail = self.reverse(head, tail) prev_node.next = new_head tail.next = _next prev_node = tail head = tail.next return dummy.next def reverse(self, root, tail): last = None tmp = root while root != tail: curr = root root = root.next curr.next = last last = curr return last, tmp if __name__ == '__main__': from problems.utils import print_list, create_list print_list(NewSolution().reverseKGroup(create_list([1, 2, 3, 4, 5]), 2)) print_list(NewSolution().reverseKGroup(create_list([1, 2]), 2)) print_list(NewSolution().reverseKGroup(create_list([1]), 2))
fast = fast.next else: return False return False class NewSolution: def hasCycle(self, head: ListNode) -> bool: fast = head if not fast or not fast.next: return False else: fast = fast.next.next slow = head.next while slow or fast: if fast == slow: return True fast = fast.next slow = slow.next if fast: fast = fast.next return False if __name__ == '__main__': from problems.utils import create_list create_list([3, 2, 0, -4])
class NewSolution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: flag = 0 ans = ListNode(-1) dummy = ans while l1 or l2: tmp = (l1.val if l1 else 0) + (l2.val if l2 else 0) + flag if tmp >= 10: tmp = tmp % 10 flag = 1 else: flag = 0 if l1: l1 = l1.next if l2: l2 = l2.next dummy.next = ListNode(tmp) dummy = dummy.next if flag: dummy.next = ListNode(flag) return ans.next if __name__ == '__main__': from problems.utils import print_list, create_list l1 = create_list([2, 4, 3]) l2 = create_list([5, 6, 4]) print_list(NewSolution().addTwoNumbers(l1, l2))
temp = head.next while temp: t = temp.next temp.next = new_head new_head = temp temp = t first.next = None return new_head class NewSolution: def reverseList(self, head: ListNode) -> ListNode: last = None while head: curr = head head = head.next curr.next = last last = curr return last if __name__ == '__main__': from problems.utils import print_list, create_list head = create_list([1, 2, 3, 4, 5]) s = NewSolution() print_list(s.reverseList(head))
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: tmp = head if not tmp: return tmp while tmp.next: if tmp.val == tmp.next.val: tmp.next = tmp.next.next else: tmp = tmp.next return head if __name__ == '__main__': from problems.utils import create_list, print_list print_list(Solution().deleteDuplicates(create_list([1, 1, 2, 3, 3]))) print_list(Solution().deleteDuplicates(create_list([ 1, ])))
break tmp_h2, pos = h, interval while h and pos: h = h.next pos -= 1 left, right = interval, interval - pos while left and right: if tmp_h1.val < tmp_h2.val: tmp.next = tmp_h1 tmp_h1 = tmp_h1.next left -= 1 else: tmp.next = tmp_h2 tmp_h2 = tmp_h2.next right -= 1 tmp = tmp.next tmp.next = tmp_h1 if left else tmp_h2 while left > 0 or right > 0: tmp = tmp.next left -= 1 right -= 1 tmp.next = h interval *= 2 return new_head.next if __name__ == '__main__': from problems.utils import create_list, print_list # print_list(NewSolution().sortList(create_list([-1, 5, 3, 4, 0]))) print_list(NewSolution().sortList(create_list([4, 2, 1, 3])))
from typing import List # Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def reversePrint(self, head: ListNode) -> List[int]: if not head: return [] ans = [] def reverse(root: ListNode): if root.next: reverse(root.next) ans.append(root.val) reverse(head) return ans if __name__ == '__main__': from problems.utils import create_list, print_list print(Solution().reversePrint(create_list([1, 2, 3])))
return head length = 0 tmp = head last = None while tmp: length += 1 last = tmp tmp = tmp.next pos = length - k % length # 只需一次遍历的位置 if pos == length: return head dummy = ListNode(-1) dummy.next = head tmp = dummy while pos > 0: tmp = tmp.next pos -= 1 ans = tmp.next tmp.next = None last.next = head return ans if __name__ == '__main__': from problems.utils import create_list, print_list print_list(Solution().rotateRight(create_list([1, 2, 3, 4, 5]), 2)) print_list(Solution().rotateRight(create_list([]), 1))
self.val = x self.next = None class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: res = ListNode(-1) temp = res while l1 and l2: if l1.val < l2.val: temp.next = ListNode(l1.val) l1 = l1.next else: temp.next = ListNode(l2.val) l2 = l2.next temp = temp.next if l1: temp.next = l1 if l2: temp.next = l2 return res.next if __name__ == '__main__': l1 = create_list([1, 2, 4]) l2 = create_list([1, 3, 4]) print_list(Solution().mergeTwoLists(l1, l2))
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def reverseList(self, head: ListNode) -> ListNode: if not head: return head prev = None while head: tmp = head.next head.next = prev prev = head head = tmp return prev if __name__ == '__main__': from problems.utils import create_list, print_list print_list(Solution().reverseList(create_list([1, 2, 3, 4, 5]))) print_list(Solution().reverseList(create_list([1])))
import heapq class QueueSolution: def mergeKLists(self, lists: List[ListNode]) -> ListNode: q = [] for i in lists: heapq.heappush(q, [i.val, i]) t = ListNode(-1) temp = t while q: p = heapq.heappop(q)[1] temp.next = p if p.next: heapq.heappush(q, [p.next.val, p.next]) temp = temp.next return t.next if __name__ == '__main__': from problems.utils import create_list, print_list # listss = [ # create_list([1, 4, 5]), # create_list([1, 3, 4]), # create_list([2, 6]) # ] listss = [create_list([0]), create_list([1, 2, 3])] print_list(QueueSolution().mergeKLists(listss))