`

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.

 

    void tail(ListNode *l2,int temp){  //结尾数据进位的处理
	    while(true){
		 if(l2->next==NULL){
			    if(temp==1){
				    ListNode *c = new ListNode (1);  //这里要在堆里面分配内存,否则函数退出会被回收
				    l2->next = c;
		                 }
			   break;
		}else{  //可能会一直进位
			int a=(l2->next->val+temp)%10;
			temp=(l2->next->val+temp)/10;
			l2->next->val=a;
			l2=l2->next;
		 }
	    }
    }

    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
	    ListNode *p=l1;
	    int temp=0;
	    while(true){
		    int current=( l1->val+l2->val+temp)%10;
		    temp=( l1->val+l2->val+temp)/10;
		    l1->val=current;
		    if(l1->next!=NULL&&l2->next!=NULL){
			    l1=l1->next;l2=l2->next;
		    }else
			    break;
	    }
	    if(l1->next==NULL&&l2->next==NULL){  //二者一样长
		    if(temp==1){
			    ListNode *c = new ListNode (1);
			    l1->next=c;
		    }
	    }else if(l1->next==NULL){  //l2长于l1,先将l2连到l1上在处理尾巴
		    int c=(l2->next->val+temp)%10;
		    temp=(l2->next->val+temp)/10;
		    l2->next->val=c;
		    l1->next=l2->next;
		    l2=l2->next;
		    tail(l2,temp);
	    }else  //l1长于l2
		    tail(l1,temp);
	    return p;
    };

 

分享到:
评论

相关推荐

    LeetCode2 Add Two Numbers

    You are given two non-empty linked lists ... 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. java AC版本

    python-leetcode面试题解之两数相加AddTwoNumbers.zip

    这个“python-leetcode面试题解之两数相加AddTwoNumbers.zip”压缩包聚焦于LeetCode中的一道经典面试题——"两数相加"(Add Two Numbers)。这道题主要考察的是链表操作和基本的计算逻辑。 题目描述:给定两个非空...

    AddTwoNumbers.java

    leetcode:Add Two Numbers(java)

    java-leetcode 题解之 Add Two Numbers II.java

    java基础 java_leetcode 题解之 Add Two Numbers II.java

    java-leetcode java题解之Add Two Numbers.java

    java基础 java_leetcode java题解之Add Two Numbers.java

    C++-leetcode题解之002. Add Two Numbers.cpp

    c++ C++_leetcode题解之002. Add Two Numbers.cpp

    手绘算法力扣 2 两数相加(Add Two Numbers)

    手绘算法力扣 2 两数相加(Add Two Numbers)

    leetcode add two numbers

    自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;...

    AddTwoNumbers

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

    addTwoNumbers_leetcode_

    给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...

    js代码-2. Add Two Numbers

    Add Two Numbers”表明这是一个关于JavaScript编程的项目,专注于将两个数字相加的功能。在JavaScript中,这是一个基础但重要的概念,它涉及到变量、操作符以及基本的算术运算。 首先,我们要了解JavaScript中的...

    0-leetcode:leetcode练习:twosum问题;addtwonumbers问题;reverse integer问题;最大不重复子字符串长度问题等,详细见readme.md;

    2. **Add Two Numbers (两数相加)**: 该问题是关于链表操作的,要求将两个非负整数表示为链表形式,然后将它们相加。这需要理解链表的结构,如节点、头结点、指针等,以及如何在链表上进行加法运算。C++中,我们可以...

    leetcode2sumc-2021-LeetCode-02_Add_Two_Numbers:2021-LeetCode-02_Add_Two

    leetcode 2 和 c 2021-LeetCode-02_Add_...addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { guard l1 != nil && l2 != nil else { return nil } var resultTail = ListNode() let resultHead = resu

    2. Add Two Numbers

    # Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None #Use listnode to get a decimal number class GetNum: snode = None num = 0 ...

    add-two-numbers

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

    程序员面试宝典LeetCode刷题手册

    2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum ...

    js重载资料

    addTwoNumbers: addTwoNumbers, addArrayElements: addArrayElements }; })(); // 调用相应的函数 console.log(add.addTwoNumbers(1, 2)); // 输出:3 console.log(add.addArrayElements([1, 2, 3])); // 输出:...

    Go 零基础2000题 从入门到精通

    说明 ⽬录 第⼀章 序章 关于 LeetCode 什么是 Cookbook 为什么会写这个开源书 关于书的封⾯ 关于作者 关于书中的代码 ...2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. ......

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    Add two numbers without using arithmetic operators Chapter 10. Given an array of integers where all the numbers are appearing twice find the only number which appears once Chapter 11. Given an array ...

Global site tag (gtag.js) - Google Analytics