Add Two Numbers - C++链表操作

题目意思很简单,两个链表分别表示两个数,将两个数相加的结果存入一个新的链表中。

思路同样很简单:两个链表如果一样长,对应位置相加,如果某一个链表多了,则根据加的结果有无进位继续处理,全部结束后要考虑会不会还剩进位。

c++的链表,题目已经给了一个挺好的例子:

struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
};

对于创建链表可以使用一个头节点来一直指向这个链表,头节点中没有数据

ListNode *l1,*l1head;
l1=new ListNode(0);
l1head=l1;

for(int i=0;i<n;i++)
{
    scanf("%d",&temp);
    ListNode *tempnode=new ListNode(temp);
    l1->next=tempnode;
    l1=l1->next;
}

接下来直接进行比较,情况考虑周全即可,比较危险的数据有

[5],[5] ; [1],[99]

 1 class Solution {
 2 public:
 3     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
 4         ListNode *res, *head;
 5         res = new ListNode(0);
 6         head = res;
 7         int flag = 0;
 8         while (l1 != NULL && l2 != NULL) {
 9             ListNode *tempNode = new ListNode(0);
10             int temp = l1->val + l2->val + flag;
11             if (temp >= 10) {
12                 flag = 1;
13                 tempNode->val = temp % 10;
14             } else {
15                 flag = 0;
16                 tempNode->val = temp;
17             }
18
19             l1 = l1->next;
20             l2 = l2->next;
21             res->next = tempNode;
22             res = res->next;
23         }
24         while (l1 != NULL) {
25             ListNode *tempNode = new ListNode(0);
26
27             if (flag == 1) {
28
29                 tempNode->val = l1->val + flag;
30                 if (tempNode->val >= 10) {
31                     flag = 1;
32                     tempNode->val = tempNode->val % 10;
33                 } else {
34                     flag = 0;
35                 }
36                 l1 = l1->next;
37                 res->next = tempNode;
38                 res = res->next;
39             } else {
40                 res->next = l1;
41                 break;
42             }
43
44         }
45         while (l2 != NULL) {
46             ListNode *tempNode = new ListNode(0);
47
48             if (flag == 1) {
49
50                 tempNode->val = l2->val + flag;
51                 if (tempNode->val >= 10) {
52                     flag = 1;
53                     tempNode->val = tempNode->val % 10;
54                 } else {
55                     flag = 0;
56                 }
57                 l2 = l2->next;
58                 res->next = tempNode;
59                 res = res->next;
60             } else {
61                 res->next = l2;
62                 break;
63             }
64
65         }
66         if (flag == 1) {
67             ListNode *tempNode = new ListNode(1);
68             res->next = tempNode;
69         }
70         return head->next;
71     }
72 };

Add Two Numbers

PS :

调用和返回都使用了p->next的方式,因为没有把头指针传进去。

时间: 2024-12-15 12:16:46

Add Two Numbers - C++链表操作的相关文章

LeetCode 2 Add Two Numbers(链表操作)

题目来源:https://leetcode.com/problems/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 li

leetcode_2_题——Add Two Numbers(链表)

Add Two Numbers Total Accepted: 58510 Total Submissions: 268082My Submissions Question Solution 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 digi

[LeetCode-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) + (5 -> 6 ->

[LeetCode]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) + (5 -> 6 ->

leetcode-2 Add Two Numbers 计算链表两个对应和的问题

 1.问题描述: You are given two linked lists representing two non-negativenumbers. The digits are stored in reverse order and each of their nodes containa single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 ->

[C++]LeetCode: 108 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 - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文: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

leetcode 5. 两个链表逐个元素相加 Add Two Numbers

7问题:Add Two Numbers 难度-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 single digit. Add the two numbers and return it as a linked list. Input: (2 -

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