【JAVA、C++】LeetCode 002 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
解题思路:
定义三个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;
             if (l1->val >= ) {
                 l1->val %= ;
                 if (l1->next == NULL)
                     l1->next = new ListNode();
                 else l1->next->val += ;
             }
             if (l2->next == NULL) {
                 ListNode* l3 = l1->next;
                 while (true) {
                     if (l3 == NULL) break;
                     if (l3->val == ) {
                         l3->val %= ;
                         if (l3->next == NULL) l3->next = new ListNode();
                         else l3->next->val += ;
                     }
                     l3 = l3->next;
                 }
                 break;
             }
             l2 = l2->next;
             if (l1->next == NULL) {
                 l1->next = new ListNode();
             }
             l1 = l1->next;
         }
         return res;
     }
 };
【JAVA、C++】LeetCode 002 Add Two Numbers的更多相关文章
- 【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 ... 
- 【JAVA、C++】LeetCode 022 Generate Parentheses
		Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ... 
- 【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 ... 
- 【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 ... 
- 【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 ... 
- 【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 ... 
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
		Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ... 
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
		Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ... 
- 【JAVA、C++】LeetCode 007 Reverse Integer
		Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ... 
随机推荐
- BZOJ-1951   古代猪文    (组合数取模Lucas+中国剩余定理+拓展欧几里得+快速幂)
			数论神题了吧算是 1951: [Sdoi2010]古代猪文 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1573 Solved: 650 [Submit ... 
- java分页
			package entity; public class Page { //记录当前页的状态信息 private int num; //当前页号,采用自然数计数 1,2,3,... private i ... 
- POJ2676Sudoku(类似于八皇后)
			Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16444 Accepted: 8035 Special ... 
- jquery------导入jquery.2.2.3.min.js
			问题: 导入jquery.2.2.3.min.js后MyEclipse会提示代码有错误 方法: 选中jquery.2.2.3.min.js->右键->选择“MyEclipse”中的“Exc ... 
- configure: error: Please reinstall the libcurl distribution
			configure: error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/ 基本上 ... 
- MongoDB的增删改查 转
			MongoDB的增删改查 (黎明你好原创作品,转载请注明) MongoDB中数据的基本单元叫做文档,采用json的键-值的方式.多个键及其关联的值有序的存放在一起变是文档.类似于编程语言中的键值关系. ... 
- hdu 2085 核反应堆
			看完题,想到用结构体存储高质点和低质点,然后打表存储<33的质点数量. #include<stdio.h> struct hilo { long long hi,lo; }; int ... 
- Repository
			namespace MyRepository.Domain.Infrastructure { public class Repository<TEntity> : IRepository& ... 
- PHP CALC
			<html> <head> <title>PHP计算器</title> <meta http-equiv="Content-Type&q ... 
- mysql join详解
			下面是例子分析 表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 a20050114 5 a20050115 表B记录如下: bID bNa ... 
