Monday, June 30, 2014

[Leetcode] Add Two Numbers

Problem

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Similar to the Add Binary Number
See: http://leetcodewz.blogspot.com/?zx=9c74695cd644c879

Follow up: 

What if the digits are stored not in reverse order, but in sequential order ? For example:
Input: (1 -> 2 -> 3) + (4 -> 5)
Output: 1 -> 6 -> 8

1. We can reverse two lists first, then call above algorithm as a subroutine.
2. Or

Code

No comments:

Post a Comment