def test_2_add(self): for case in self.cases: l1, l2, result = case n1 = ListNode.of(l1) n2 = ListNode.of(l2) res = ListNode.of(result) self.assertEqual(self.solution.addTwoNumbers(n1, n2), res)
while p1 is not None and p2 is not None: if p1.val != p2.val: ret = False break p1 = p1.next p2 = p2.next # revert back prev = tail_head cur = front_head while cur is not None: nxt = cur.next cur.next = prev prev = cur cur = nxt return ret solution = Solution() x = ListNode.of([1, 2, 3, 2, 1]) assert solution.isPalindrome(x) assert x == [1, 2, 3, 2, 1] y = ListNode.of([1, 2, 2, 1]) assert solution.isPalindrome(y) assert y == [1, 2, 2, 1] z = ListNode.of([]) assert solution.isPalindrome(z)
# -*- coding: utf-8 -*- from util import ListNode class Solution: def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None: return head cur = head prev = None while cur is not None: nxt = cur.next cur.next = prev prev = cur cur = nxt return prev x = ListNode.of([1, 2, 3, 4]) solution = Solution() y = solution.reverseList(x) assert y == [4, 3, 2, 1]