2、LeetCode--Add Two Numbers

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

中文描述:给你两个链表代表两个非负的数字。每个数字存储在倒转顺序的列表中,并且每个节点包含一个十进制的数,将这两个数相加并返回该格式的链表。

example的意思: 342 + 465 = 807

分析:由于第一个节点代表第一位,我们可以采用进位相加的方法。

    有3种可能出现的情况。

    1、两个链表一样长,这时我们要注意最高为位产生进位,如果产生进位,则要在相加的结果上增加一个为1的node节点。

    2、第一个链表长度大于第二个链表,将第一个链表超出的部分增加到sum链表中,注意与第二个链表最高位相加产生的进位。

    3、第二个链表长度大于第一个链表,与情况二类似,不再赘述。

LeetCode代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    int carry = 0;                    // 进位
    struct ListNode *p1, *p2;
    struct ListNode *p, *q;
    p = q = NULL;
    p1 = l1; p2 = l2;

    int current;
    int flag = 1;
    struct ListNode *node;
    while(p1 && p2)                   // 同步遍历链表
    {
        current = p1->val + p2->val;
        current += carry;             // 加上进位
        if(current >= 10)             // 产生进位
        {
            current -= 10;
            carry = 1;
        }
        else
            carry = 0;

        node = (struct ListNode *)malloc(sizeof(struct ListNode));
        node->val = current;
        node->next = NULL;
        if(flag)                       // 第一次添加节点
        {
            p = node;
            q = node;
            flag = 0;
        }
        else
        {
            q->next = node;
            q = node;
        }

        p1 = p1->next;
        p2 = p2->next;
    }

    if(p1)                            // 链表1长度大于链表2
    {
        while(p1)
        {
            current = p1->val + carry;
            if(current >= 10)
            {
                current -= 10;
                carry = 1;
            }
            else
                carry = 0;

            node = (struct ListNode *)malloc(sizeof(struct ListNode));
            node->val = current;
            node->next = NULL;
            q->next = node;
            q = node;

            p1 = p1->next;
        }
    }

    if(p2)							// 链表2长度大于链表1
    {
        while(p2)
        {
            current = p2->val + carry;
            if(current >= 10)
            {
                current -= 10;
                carry = 1;
            }
            else
                carry = 0;

            node = (struct ListNode *)malloc(sizeof(struct ListNode));
            node->val = current;
            node->next = NULL;
            q->next = node;
            q = node;

            p2 = p2->next;
        }
    }

    if(carry == 1)                           // 链表1长度等于链表2,并且最高位相加产生进位
    {
        node = (struct ListNode *)malloc(sizeof(struct ListNode));
        node->val = 1;
        node->next = NULL;
        q->next = node;
        q = node;
    }

    return p;
}

如果有更好的方法,希望不吝赐教!

时间: 2024-10-11 18:32:10

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