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

解题思路:

定义三个ListNode l1、l2,result,其中result为return语句的输出,l1、l2为传入的参数。

将l1赋值给result,执行result.val+=l2.val,然后l1作为指针一级一级往下走,直到走到l2.next为null。当然,之间会有不少边界条件,自己debug一下就好。

Java代码如下:

public class Solution {
      static public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    	ListNode result=l1;
    	while(true){
    		l1.val+=l2.val;
    		if(l1.val>=10){
    			l1.val%=10;
    			if(l1.next==null) l1.next=new ListNode(1);
    			else l1.next.val+=1;
    		}

		if(l2.next==null){
			ListNode l3=l1.next;
			while(true){
				if (l3==null) break;
				if(l3.val==10){
					l3.val%=10;
	    			if(l3.next==null) l3.next=new ListNode(1);
	    			else l3.next.val+=1;
				}
				l3=l3.next;
			}
			break;
		}

    	l2=l2.next;
    	if(l1.next==null){
    		l1.next=new ListNode(0);
    	}
    	l1=l1.next;
    	}
    	return result;

    }
}

C++

 class Solution {
 public:
     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
         ListNode* res=l1;
         while (true) {
             l1->val += l2->val;
             ) {
                 l1->val %= ;
                 if (l1->next == NULL)
                     l1->next = );
                 ;
             }

             if (l2->next == NULL) {
                 ListNode* l3 = l1->next;
                 while (true) {
                     if (l3 == NULL) break;
                     ) {
                         l3->val %= ;
                         );
                         ;
                     }
                     l3 = l3->next;
                 }
                 break;
             }

             l2 = l2->next;
             if (l1->next == NULL) {
                 l1->next = );
             }
             l1 = l1->next;
         }
         return res;
     }
 };

【JAVA、C++】LeetCode 002 Add Two Numbers的更多相关文章

  1. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  3. 【JAVA、C++】LeetCode 001 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. 【JAVA、C++】LeetCode 018 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  5. 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. 【JAVA、C++】LeetCode 015 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  7. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  9. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

随机推荐

  1. 0103MySQL中的B-tree索引 USINGWHERE和USING INDEX同时出现

    转自博客http://www.amogoo.com/article/4 前提1,为了与时俱进,文中数据库环境为MySQL5.6版本2,为了通用,更为了避免造数据的痛苦,文中所涉及表.数据,均来自于My ...

  2. Android WebView使用

    转自:http://www.cnblogs.com/oakpip/archive/2011/04/08/2009800.html 大部分内容为网上整理其它高人的帖子,现只作整理,用于查看: 在Andr ...

  3. 面试题目——《CC150》线程与锁

    package cc150.thread_lock; public class RunnableThreadExample implements Runnable{ public int count ...

  4. 前端MV*框架的意义

    经常有人质疑,在前端搞MV*有什么意义?也有人提出这样的疑问:以AngularJS,Knockout,BackBone为代表的MV*框架,它跟jQuery这样的框架有什么区别?我jQuery用得好好的 ...

  5. qsort函数、sort函数【转】

    http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...

  6. perl 箭头操作符

    箭头操作符有两种用法. 第一种用法是访问引用中的数据元素: #!/usr/bin/perl -w use strict; my @a = (5, 6, 7,9); my $aref = \@a; pr ...

  7. SWFUpload 2.5.0版 官方说明文档 中文翻译版

    原文地址:http://www.cnblogs.com/youring2/archive/2012/07/13/2590010.html#setFileUploadLimit SWFUpload v2 ...

  8. ajax跨域请求带cookie

    调用网站:a.xxx.com jQuery(document).ready(function () { $.ajax({ type: "get", async: true, url ...

  9. SQLSERER给表加自增列

    alter table 表名 add 列名 int IDENTITY(1,1) NOT NULL

  10. 解决This system is not registered with RHN

    Redhat之所以会出现这个错误是因为没有注册RHN,我们只需要更新一下yum的源就可以了.使用命令 cd /etc/yum.repos.d/   进入yum的配置目录. 在终端中输入 wget ht ...