码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 2 Add Two Numbers

时间:2015-05-05 19:36:52      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:leetcode   java   c   c++   python   

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

Solution:


题目大意:

给定两个单链表,每一个链表结点有一个0-9的数字,链表代表一个整数的倒序排列,要求将两个链表相加并且结果也返回为和的倒序排列方式,例如Problem中的Input和Output

解题思路:

主要就是模拟大整数相加,注意进位问题和单链表的操作即可,题目简单,注意细节

Java源代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int high=(l1.val+l2.val)/10;
        ListNode head = new ListNode((l1.val+l2.val)%10);
        ListNode p=head;
        l1=l1.next;l2=l2.next;
        while(l1!=null|| l2!=null){
            int a =0 l1==null?0:l1.val;
            int b =0 l2==null?0:l2.val;
            ListNode s=new ListNode((a+b+high)%10);
            high = (a+b+high)/10;
            p.next=s;
            p=s;
            l1 = l1==null?null:l1.next;
            l2 = l2==null?null:l2.next;
        }
        if(high!=0){
            ListNode s=new ListNode(high);
            p.next=s;
            p=s;
        }
        return head;
    }
}

C语言源代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
#include<stdio.h>
#include<stdlib.h>
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    int high=(l1->val+l2->val)/10;
    struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* p=head;
    struct ListNode* s;
    head->val = (l1->val+l2->val)%10;
    head->next=NULL;
    l1=l1->next;l2=l2->next;
    while(l1!=NULL || l2!=NULL){
        int a = l1==NULL?0:l1->val;
        int b = l2==NULL?0:l2->val;
       s = (struct ListNode*)malloc(sizeof(struct ListNode));
       s->val = (a+b+high)%10;
       s->next=NULL;
       p->next=s;
       p=s;
       high = (a+b+high)/10;
       l1 = l1==NULL?NULL:l1->next;
       l2 = l2==NULL?NULL:l2->next;
    }
    if(high!=0){
       s = (struct ListNode*)malloc(sizeof(struct ListNode));
       s->val = high;
       s->next=NULL;
       p->next=s; 
    }
    return head;
}

C++源代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
#include<stdio.h>
#include<stdlib.h>
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
       int high = (l1->val+l2->val)/10;
       struct ListNode* head = new ListNode((l1->val+l2->val)%10);
       head->next=NULL;
       struct ListNode* p;
       p=head;
       l1=l1->next;l2=l2->next;
       while(l1!=NULL || l2!=NULL){
           int a = l1==NULL?0:l1->val;
           int b = l2==NULL?0:l2->val;
           struct ListNode* s = new ListNode((a+b+high)%10);
           s->next=NULL;
           p->next=s;
           p=s;
           high = (a+b+high)/10;
           l1 = l1==NULL?NULL:l1->next;
           l2 = l2==NULL?NULL:l2->next;
       }
       if(high){
           struct ListNode* s = new ListNode(high);
           s->next=NULL;
           p->next=s;
       }
       return head;
    }
};

Python源代码:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param {ListNode} l1
    # @param {ListNode} l2
    # @return {ListNode}
    def addTwoNumbers(self, l1, l2):
        high = (l1.val+l2.val)/10;
        head = ListNode((l1.val+l2.val)%10)
        p=head;
        l1=l1.next
        l2=l2.next
        while l1!=None or l2!=None:
            a =0 if l1==None else l1.val
            b =0 if l2==None else l2.val
            s = ListNode((a+b+high)%10)
            s.next=None
            p.next=s
            p=s
            high = (a+b+high)/10
            l1 =None if l1==None else l1.next
            l2 =None if l2==None else l2.next
        if high!=0:
            s = ListNode(high)
            s.next=None
            p.next=s
        return head


LeetCode 2 Add Two Numbers

标签:leetcode   java   c   c++   python   

原文地址:http://blog.csdn.net/runningtortoises/article/details/45505943

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!