Saturday, August 30, 2014

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

Idea

  • 要考虑到两个Number可能长短不一
  • 要考虑到最高位可能产生的进位

Solution


Special Cases

  • {2,8,9,9,9,9,8,9,9,9}, {8,1,2} // 有可能不一样长啊

No comments:

Post a Comment