leetcode3--Add Two Numbers

<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);">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.</span>

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

题解:给定两个有序链表,将两个链表中对应节点得数据相加,进位制加入到下一个节点中,返回相加后得节点。

思路:首先想到解题得思路是,用两个指针遍历两个链表,取每个节点上得数据,进行相加,考虑是进位是否需要添加到下一个节点中。这其中应注意每个链表结束得判断,因为题目中为给出两个链表得长度是否相等。还有最后一次判断是否有进位,如果有得话就需要添加节点,具体代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode* ltmp = new ListNode(0);
        ListNode* pt = ltmp; //保存链表头
			int vtmp = 0;
			int htmp = 0;

			while(l1 !=NULL || l2 != NULL)
			{
			    if(l1 !=NULL)
			    {
			        vtmp += l1->val;
			        l1 = l1->next;
			    }
			    if(l2 != NULL)
			    {
			        vtmp += l2->val;
			        l2 = l2->next;
			    }
			    ltmp->next = new ListNode(vtmp % 10);
			    htmp = vtmp/10;
			    vtmp =htmp;
			    ltmp = ltmp->next;
			}
			if(htmp == 1)
			ltmp->next = new ListNode(1);
			return pt->next;
    }

};

在提交得时总出现一个错误,返回得结果为空,经过仔细得分析代码发现,之前返回得是ltmp->next,但是该指针已经是走后一个节点了,结果肯定为空,所以在前文需要另外定义一个指针存储链表得头指针,用于准确返回结果。

时间: 2024-10-12 04:50:59

leetcode3--Add Two Numbers的相关文章

2、Add Two Numbers

1.Add Two Numbers--这是leedcode的第二题: 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:

LeetCode --- 2. Add Two Numbers

题目链接: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

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

Add Two Numbers(Linked List)

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) + (

Add Two Numbers

题目: 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. 程序设计 链表节点定义 Def

Leetcode 02 Add Two Numbers

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) + (

【Leet Code】Add Two Numbers

Add Two Numbers Total Accepted: 20255 Total Submissions: 88115My Submissions 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 numb

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速度才是王道 2. Add Two Numbers

2. 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)

leetcode 002 Add Two Numbers

Add Two Numbers My Submissions Question Total Accepted: 105768 Total Submissions: 494750 Difficulty: Medium 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