LeetCode题解(2)--Add Two Numbers

来源:互联网 发布:jquery ajax 提交json 编辑:程序博客网 时间:2024/06/03 20:22

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8

解题思路

因为整数是逆序存储,所以逐位相加,得到相应的和及进位,若其中一链表循环结束,其值以0代替。

解题代码

C++

#include <iostream>using namespace std;struct ListNode {    int val;    ListNode* next;    ListNode(int x) : val(x),next(NULL) {}};class Solution {public:    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {        ListNode head(0);        ListNode* cur = &head;        int extra = 0;        while (l1 || l2 || extra ) {            int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + extra;            extra = sum / 10;            cur->next = new ListNode(sum % 10);            cur = cur->next;            if(l1) l1 = l1->next;            if(l2) l2 = l2->next;        }        return head.next;    }};ListNode* createList() {    ListNode head(0);    ListNode* cur = &head;    int x;    while(cin >> x){        ListNode* temp = new ListNode(x);        cur->next = temp;        cur = cur->next;    }    return head.next;}void printList(ListNode* head) {    ListNode* p = head;    while(p){        cout << p->val << '\t';        p = p->next;    }}int main(){    ListNode* l1 = createList();    cin.clear();//更改cin的状态标示符    cin.sync();//清除缓存区的数据流    ListNode* l2 = createList();    Solution solution;    ListNode* head = solution.addTwoNumbers(l1,l2);    printList(head);    return 0;}

Java

package leetcode;import java.util.Scanner;class ListNode {    int val;    ListNode next;    ListNode(int x) { val = x; }}public class Solution {    public static ListNode addTwoNumbers(ListNode l1 , ListNode l2) {        ListNode head = new ListNode(0);        ListNode cur = head;        int extra = 0;        while(l1 != null || l2 != null || extra != 0){            int sum = ((l1 != null) ? l1.val : 0) + ((l2 != null) ? l2.val : 0) + extra;            extra = sum / 10;            cur.next = new ListNode(sum % 10);            cur = cur.next;            if(l1 != null) l1 = l1.next;            if(l2 != null) l2 = l2.next;        }        return head.next;    }    public static ListNode createList() {        ListNode head = new ListNode(0);        ListNode cur = head;        Scanner scanner = new Scanner(System.in);        while(scanner.hasNextInt()) {            int x = scanner.nextInt();            ListNode temp = new ListNode(x);            cur.next = temp;            cur = cur.next;        }        return head.next;    }    public static void printList(ListNode head) {        ListNode p = head;        while(p != null) {            System.out.print(p.val+"\t");            p = p.next;        }    }    public static void main(String[] args) {        // TODO Auto-generated method stub        ListNode l1 = createList();        ListNode l2 = createList();        ListNode head = addTwoNumbers(l1, l2);        printList(head);        }}

Python

class ListNode:    def __init__(self,x):        self.val = x        self.next = Noneclass Solution:    def addTwoNumbers(self, l1, l2):        """        :type l1: ListNode        :type l2: ListNode        :rtype: ListNode        """        head = ListNode(0)        cur = head        extra = 0        while l1 or l2 or extra :            sum = (l1.val if l1 else 0) + (l2.val if l2 else 0) + extra            extra = sum // 10            temp = ListNode(sum % 10)            cur.next = temp            cur = cur.next            if l1 :                l1 = l1.next            if l2 :                l2 = l2.next        return head.nextif __name__ == '__main__' :    a , a.next , a.next.next = ListNode(2) , ListNode(4) , ListNode(3)    b , b.next , b.next.next = ListNode(5) , ListNode(6) , ListNode(4)    solution = Solution()    head = solution.addTwoNumbers(a,b)    print(head.val,head.next.val,head.next.next.val)
0 0
原创粉丝点击