print("addOne", addOne) l1 = l1.next l2 = l2.next while l1 or l2 or addOne == 1: val1 = 0 if l1 == None else l1.val val2 = 0 if l2 == None else l2.val first = (val1 + val2 + addOne) % 10 print("val1 , val2, addOne", val1, val2, first) newNode = ListNode((val1 + val2 + addOne) % 10) addOne = int((val1 + val2 + addOne) / 10) current.next = newNode current = newNode l1 = None if l1 == None else l1.next l2 = None if l2 == None else l2.next return head l1 = ListNode(2) l1.append([4, 3]) l1.printLL() l2 = ListNode(5) l2.append([6, 4]) l2.printLL() s = Solution() r = s.addTwoNumbers(l1, l2) r.printLL()
#https://leetcode.com/problems/middle-of-the-linked-list/ from linkedList import ListNode class Solution: def middleNode(self, head: ListNode) -> ListNode: if not head: return None slow, fast = head, head while fast: fast = fast.next if not fast: return slow fast = fast.next slow = slow.next if not fast: return slow return slow head = ListNode(1) head.append([2, 3]) head.printLL() s = Solution() r = s.middleNode(head) print(r.val)
# self.next = next from linkedList import ListNode class Solution: def oddEvenList(self, head: ListNode) -> ListNode: if not head: return None odd = head even = head.next evenHead = even while even and even.next: odd.next = even.next odd = even.next even.next = odd.next even = odd.next odd.next = evenHead return head head = ListNode(2) head.append([1, 3, 5, 6, 4, 7]) head.printLL() s = Solution() r = s.oddEvenList(head) r.printLL()