LeetCode 2 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

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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值