[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 -> 4)
Output: 7 -> 0 -> 8

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
12         ListNode *head = new ListNode(0);
13         ListNode *p = head, *p1 = l1, *p2 = l2;
14         int val, val1, val2;
15         int carry = 0;
16         while (p1 != NULL || p2 != NULL) {
17             val1 = val2 = 0;
18             if (p1 != NULL) {
19                 val1 = p1->val;
20                 p1 = p1->next;
21             }
22             if (p2 != NULL) {
23                 val2 = p2->val;
24                 p2 = p2->next;
25             }
26             val = val1 + val2 + carry;
27             carry = val / 10;
28             val %= 10;
29             p->next = new ListNode(val);
30             p = p->next;
31         }
32         if (carry != 0) {
33             p->next = new ListNode(carry);
34         }
35         ListNode *sum = head->next;
36         delete head;
37         return sum;
38     }
39 };
时间: 2024-10-13 00:12:01

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