classSolution: defreverseList(self, head: ListNode) -> ListNode: prev, curr = None, head while curr isnotNone: next = curr.next curr.next = prev prev = curr curr = next return prev
defaddTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: a = [] b = [] c = 0 m = n = ListNode(0) k = 0 v = 0 while l1 or l2: if l1: a.append(l1.val) l1 = l1.next
if l2: b.append(l2.val) l2 = l2.next while a or b: if a: k = a.pop() else: k = 0
if b: v = b.pop() else: v = 0
s = (k+v+c) c = s // 10 m.next = ListNode(s % 10) m = m.next if c: m.next = ListNode(c) return self.reverseList(n.next)
## 时间复杂度: O(max(n,m)) ## 空间复杂度: O(m+n) classSolution: defreverseList(self, head: ListNode) -> ListNode: prev, curr = None, head while curr isnotNone: next = curr.next curr.next = prev prev = curr curr = next return prev
defaddTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: a = [] b = [] c = 0 m = n = ListNode(0) k = 0 v = 0 while l1 or l2: if l1: a.append(l1.val) l1 = l1.next
if l2: b.append(l2.val) l2 = l2.next while a or b: if a: k = a.pop() else: k = 0
if b: v = b.pop() else: v = 0
s = (k+v+c) c = s // 10 m.next = ListNode(s % 10) m = m.next if c: m.next = ListNode(c) return self.reverseList(n.next)