Ejemplo n.º 1
0
            over, nxt_val = nxt_val / 10, nxt_val % 10
            curr.next = ListNode(nxt_val)
            curr = curr.next
            l1, l2 = l1.next, l2.next
            
        if over > 0:
            curr.next = ListNode(over)
        return res.next if res.next else None


if __name__ == '__main__':
    # l1 = 2 -> 4 -> 3
    # l2 = 5 -> 6 -> 4
    # ans = 7 -> 0 -> 8
    l1 = LinkedList()
    l1.toLinkedList([2,4,3])
    l2 = LinkedList()
    l2.toLinkedList([5,6,4])

    l1.toLinkedList([3,4])
    l2.toLinkedList([8,6,0,0,5])
    resNode = Solution().addTwoNumbers(l1.head, l2.head)
    res = LinkedList(resNode)
    res.printLL()

    l1.toLinkedList([1,8])
    l2.toLinkedList([0])
    resNode = Solution().addTwoNumbers(l1.head, l2.head)
    res = LinkedList(resNode)
    res.printLL()
Ejemplo n.º 2
0
from utils import ListNode, LinkedList

class Solution(object):
    def removeNthFromEnd(self, head, n):
        first, second = head, None
        count = 0
        while first:
            if count == n and not second:
                second = head
            elif count > n:
                second = second.next
            first = first.next
            count += 1
        if not second: return head.next
        second.next = second.next.next
        return head

if __name__ == '__main__':
    ll = LinkedList()
    ll.toLinkedList([1,2,3,4,5])
    res = Solution().removeNthFromEnd(ll.head, 2)
    resLL = LinkedList(res)
    resLL.printLL()