from LeetTool import build_ListNode class Solution: def plusOne(self, head: ListNode) -> ListNode: lst = [] ans = node = ListNode(0) node.next = head while node: lst.append(node) node = node.next while lst: if lst[-1].val < 9: lst[-1].val += 1 break else: lst[-1].val = 0 lst.pop() if ans.val == 0: return ans.next else: return ans if __name__ == "__main__": # [1,2,4] print(Solution().plusOne(build_ListNode([1, 2, 3])))
from LeetTool import build_ListNode class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: head = node = ListNode(0) last = 0 # 进位数量 while l1 or l2 or last: now = last if l1: now += l1.val l1 = l1.next if l2: now += l2.val l2 = l2.next last, now = divmod(now, 10) node.next = ListNode(now) node = node.next return head.next if __name__ == "__main__": print(Solution().addTwoNumbers(build_ListNode([7, 1, 6]), build_ListNode([5, 9, 2]))) # 2->1->9 print(Solution().addTwoNumbers(build_ListNode([6, 1, 7]), build_ListNode([2, 9, 5]))) # 9->1->2
if mm: node = node.next mm -= 1 elif nn: if node.next: node.next = node.next.next else: node.next = None nn -= 1 else: mm, nn = m, n return head if __name__ == "__main__": # [1,2,6,7,11,12] print(Solution().deleteNodes( build_ListNode([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]), 2, 3)) # [1,5,9] print(Solution().deleteNodes( build_ListNode([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), 1, 3)) # [1,2,3,5,6,7,9,10,11] print(Solution().deleteNodes( build_ListNode([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), 3, 1)) # [9,7,8] print(Solution().deleteNodes(build_ListNode([9, 3, 7, 7, 9, 10, 8, 2]), 1, 2))
def isPalindrome(self, head: ListNode) -> bool: i1 = i2 = head # 快慢针求中点 while i1 and i1.next: i1 = i1.next.next i2 = i2.next # 翻转链表 i3 = None while i2: last = i2.next i2.next = i3 i3 = i2 i2 = last # 比较链表 i4 = head while i3 and i4: if i3.val != i4.val: return False i3 = i3.next i4 = i4.next return True if __name__ == "__main__": print(Solution().isPalindrome(build_ListNode([1, 2]))) # False print(Solution().isPalindrome(build_ListNode([1, 2, 2, 1]))) # True
from LeetTool import ListNode from LeetTool import build_ListNode class Solution: def kthToLast(self, head: ListNode, k: int) -> int: i1, i2 = head, head for _ in range(k - 1): i1 = i1.next while i1.next: i1 = i1.next i2 = i2.next return i2.val if __name__ == "__main__": print(Solution().kthToLast(build_ListNode([1, 2, 3, 4, 5]), 2)) # 4
from LeetTool import ListNode from LeetTool import build_ListNode class Solution: def swapNodes(self, head: ListNode, k: int) -> ListNode: n1 = n2 = head for _ in range(k - 1): n2 = n2.next n3 = n2 # 正数第k个节点 while n2.next: n1 = n1.next n2 = n2.next n4 = n1 # 倒数第k个节点 n3.val, n4.val = n4.val, n3.val return head if __name__ == "__main__": print(Solution().swapNodes(build_ListNode([1, 2, 3, 4, 5]), k=2)) print(Solution().swapNodes(build_ListNode([7, 9, 6, 6, 7, 8, 3, 0, 9, 5]), k=5)) print(Solution().swapNodes(build_ListNode([1]), k=1)) print(Solution().swapNodes(build_ListNode([1, 2]), k=1)) print(Solution().swapNodes(build_ListNode([1, 2, 3]), k=2))
import random from LeetTool import ListNode from LeetTool import build_ListNode class Solution: def __init__(self, head: ListNode): self.head = head def getRandom(self) -> int: now = 0 idx = 0 node = self.head while node: idx += 1 rand = random.randint(1, idx) if rand == idx: now = node.val node = node.next return now if __name__ == "__main__": obj = Solution(build_ListNode([1, 2, 3])) print(obj.getRandom()) # 1或2或3
def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode: start, end = None, None node = list1 while node: if node.next and node.next.val == a: start = node if node.val == b: end = node.next node = node.next node = list2 while node.next: node = node.next node.next = end start.next = list2 return list1 if __name__ == "__main__": # [0,1,2,1000000,1000001,1000002,5] print(Solution().mergeInBetween( build_ListNode([0, 1, 2, 3, 4, 5]), 3, 4, build_ListNode([1000000, 1000001, 1000002]))) # [0,1,1000000,1000001,1000002,1000003,1000004,6] print(Solution().mergeInBetween( build_ListNode([0, 1, 2, 3, 4, 5, 6]), 3, 4, build_ListNode([1000000, 1000001, 1000002, 1000003, 1000004])))
from LeetTool import ListNode from LeetTool import build_ListNode class Solution: def partition(self, head: ListNode, x: int) -> ListNode: head1 = node1 = ListNode(0) head2 = node2 = ListNode(0) while head: if head.val >= x: node2.next = ListNode(head.val) node2 = node2.next else: node1.next = ListNode(head.val) node1 = node1.next head = head.next node1.next = head2.next return head1.next if __name__ == "__main__": # 3->1->2->10->5->5->8 print(Solution().partition(build_ListNode([3, 5, 8, 5, 10, 2, 1]), 5))
from LeetTool import ListNode from LeetTool import build_ListNode class Solution: def deleteNode(self, node): node.val = node.next.val node.next = node.next.next if __name__ == "__main__": listNode = build_ListNode(["a", "b", "c", "d", "e", "f"]) print(Solution().deleteNode(listNode.next.next)) print(listNode)