LeetCode---------Add Two Numbers 解法

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.

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

  题目如上述所示。

  大概翻译:

  给出两个非空的链表代表两个非负的整数。数字以相反的顺序存储,每个节点包含一个数字,将两个数相加并把结果作为一个链表返回。

  你可以假设两个数除了本身是0以外都没有前导0。

  本题最关键的地方在于解决进位问题。

  在网上查询了一些解法,包括借鉴了这篇:http://blog.csdn.net/ljiabin/article/details/40476399

  其中对于进位问题的解决在一开始便引入一个节点以便最后有进位时使用个人觉得有些不妥,并且使代码不太容易懂。

  下面给出我对于此问题的解法:

  【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) {
        //如果给出就为空,则直接返回另外一个链表
        if(l1 == null) return l2;
        if(l2 == null) return l1;

        int flag = 0;//存放进位信息,但是并不是处理最后的进位标志                            

        //构造返回结果的第一个节点
        ListNode result = new ListNode((l1.val + l2.val) % 10);
        ListNode p = result;
        flag = (l1.val + l2.val) / 10;

        l1 = l1.next;
        l2 = l2.next;

        while(l1!=null || l2!=null){
            int l1Num = (l1==null)?0:l1.val;//如果l1链表为空,则视值为0
            int l2Num = (l2==null)?0:l2.val;//如果l2链表为空,则视值为0
            p.next = new ListNode((l1Num + l2Num + flag) % 10);
            p = p.next;
            flag = (l1Num + l2Num + flag) / 10;
            if(l1 != null){
                l1 = l1.next;
            }
            if(l2 != null){
                l2 = l2.next;
            }

        }
      //处理最后的进位问题
        if(flag != 0){
            p.next = new ListNode(flag);
            p = p.next;
        }

        return result;
    }
}

  

  

如果有任何问题,欢迎跟我联系:[email protected]

我的github地址:github.com/WXRain

时间: 2024-10-10 18:14:43

LeetCode---------Add Two Numbers 解法的相关文章

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

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

leetcode——Add Two Numbers 两个链表表示的正整数对其求和(AC)

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

LeetCode: Add Two Numbers 解题报告

Add Two NumbersYou 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

[LeetCode] 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 ->

[LeetCode] Add Two Numbers II 两个数字相加之二

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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 con

[LeetCode] 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 ->

leetcode add two numbers python

# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode &q

leetcode Add Two Numbers(对指针的一些基本操作)

1 ListNode *ptr,*l; 2 l = new ListNode(0);//这才是正确的赋值姿势 3 ptr = l;//赋给的是地址 4 int up = 0,fg1 = 0,fg2 = 0; 5 //cout<<"r"<<endl; 6 while(1) 7 { 8 if(fg1 && fg2) break; 9 int a,b; 10 if(fg1) a = 0; 11 else a = l1 -> val; 12 if(

[leetcode]Add Two Numbers——JS实现

Javascript的结构体应用,如下:    function station(name, latitude, longitude){        this.name = name;        this.latitude = latitude;        this.longitude = longitude;    }    var s1 = new station('station1', 100, 200);    console.log(s1.name+" 's latitude