以下問題在微軟的面試出現過:
輸入兩個連結串列(linked list)表示的非負整數,數字儲存的順序是反的,每個節點儲存單一數字。將兩個數字相加之後,回傳代表該答案的連結串列。
範例:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
以下是 Python 程式做範例的起始點:
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def addTwoNumbers(self, l1, l2, c = 0):
# Fill this in.
l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)
l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)
result = Solution().addTwoNumbers(l1, l2)
while result:
print result.val,
result = result.next
# 7 0 8