2015年12月10日 星期四

[LC2] Add Two Numbers

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

題目要求:
給定兩個 Linked Lists代表兩個非負數字,每個單獨的位數存在 List node裡,請你相加個數字,並請回傳一個 Linked List來表示答案。
思路:
個別從兩個 Lists中取值相加另外必須再加上前一個位數所進位過來的數值。每個位數中,只取 < 10的數值留下來,> 10的數值必須進位。這題注意進位的處理,就沒啥麼太大問題。
實作:

沒有留言:

張貼留言