AddTwoNumbers.java ( File view )

  • By issaczr 2014-10-24
  • View(s):52
  • Download(s):0
  • Point(s): 1
			// Author:   Li Long, 1988lilong@163.com
// Date:     Apr 18, 2014
// Source:   http://oj.leetcode.com/problems/add-two-numbers/
// Analysis: http://blog.csdn.net/lilong_dream/article/details/19544995

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

/**
 * Definition for singly-linked list.
 * public class ListNode {

 *     int val;
 *     ListNode next;
 *     ListNode(int x) {

 *         val = x;
 *         next = null;
 *     
}
 * 
}
 */

public class AddTwoNumbers {

	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

		// IMPORTANT: Please reset any member data you declared, as
		// the same Solution instance will be reused for each test case.
		if (l1 == null) {

			return l2;
		
}
		if (l2 == null) {

			return l1;
		
}

		int len1 = 0;
		int len2 = 0;

		ListNode head = l1;

		while (head != null) {

			++len1;
			head = head.next;
		
}

		head = l2;

		while (head != null) {

			++len2;
			head = head.next;
		
}

		ListNode longer = len1 >= len2 ? l1 : l2;
		ListNode shorter = len1 < len2 ? l1 : l2;

		ListNode result = null;
		ListNode sum = null;

		int val = 0;
		int carry = 0;

		while (shorter != null) {

			val = longer.val + shorter.val + carry;
			carry = val / 10;
			val -= carry * 10;
			
			if (sum == null) {

				sum = new ListNode(val);
				result = sum;
			
} else {

				sum.next = new ListNode(val);
				sum = sum.next;
			
}
			
			longer = longer.next;
			shorter = shorter.next;
		
}

		while (longer != null) {

			val = longer.val + carry;
			carry = val / 10;
			val -= carry * 10;
			
			sum.next = new ListNode(val);
			sum = sum.next;
			
			longer = longer.next;
		
}

		if (carry != 0) {

			sum.next = new ListNode(carry);
		
}

		return result;
	
}

	public void print(ListNode node) {

		while (node != null) {

			System.out.print(node.val + "->");
			node = node.next;
		
}
	
}
    
	public static void main(String[] args) {

		ListNode l1 = new ListNode(2);
		ListNode n2 = new ListNode(4);
		ListNode n3 = new ListNode(3);

		l1.next = n2;
		n2.next = n3;

		ListNode l2 = new ListNode(5);
		ListNode n4 = new ListNode(6);
		ListNode n5 = new ListNode(4);
		l2.next = n4;
		n4.next = n5;

		AddTwoNumbers slt = new AddTwoNumbers();
		ListNode result = slt.addTwoNumbers(l1, l2);
		slt.print(result);
	
}

}			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
01.97 kB
AddBinary.java1.58 kB2014-05-09 03:48
AddTwoNumbers.java2.54 kB2014-05-09 03:48
BinaryTreePreorderTraversal.java1.91 kB2014-05-09 03:48
ClimbingStairs.java811.00 B2014-05-09 03:48
EvaluateReversePolishNotation.java1.51 kB2014-05-09 03:48
GenerateParentheses.java1.15 kB2014-05-09 03:48
ImplementStrStr.java1.00 kB2014-05-09 03:48
InsertionSortList.java1.64 kB2014-05-09 03:48
IntegerToRoman.java923.00 B2014-05-09 03:48
LengthOfLastWord.java1.11 kB2014-05-09 03:48
LinkedListCycle.java1.32 kB2014-05-09 03:48
LinkedListCycleII.java1.28 kB2014-05-09 03:48
LongestCommonPrefix.java916.00 B2014-05-09 03:48
LongestSubstring.java1.79 kB2014-05-09 03:48
MaximumSubarray.java1.10 kB2014-05-09 03:48
MedianOfTwoSortedArrays.java1.68 kB2014-05-09 03:48
MergeSortedArray.java1.12 kB2014-05-09 03:48
MergeTwoSortedLists.java1.84 kB2014-05-09 03:48
MinimumPathSum.java1.19 kB2014-05-09 03:48
PathSum.java1.67 kB2014-05-09 03:48
PlusOne.java1.06 kB2014-05-09 03:48
Powxn.java712.00 B2014-05-09 03:48
RemoveDuplicatesFromSortedList.java1.48 kB2014-05-09 03:48
RemoveDuplicatesfromSortedArray.java1.30 kB2014-05-09 03:48
RemoveElement.java885.00 B2014-05-09 03:48
RemoveNthNodeFromEnd.java1.70 kB2014-05-09 03:48
ReverseInteger.java936.00 B2014-05-09 03:48
RotateList.java1.53 kB2014-05-09 03:48
SameTree.java1.15 kB2014-05-09 03:48
SearchForARange.java1.37 kB2014-05-09 03:48
SearchInRotatedSortedArray.java1.27 kB2014-05-09 03:48
SearchInsertPosition.java1.09 kB2014-05-09 03:48
SetMatrixZeroes.java1.95 kB2014-05-09 03:48
SingleNumber.java935.00 B2014-05-09 03:48
SortList.java1.75 kB2014-05-09 03:48
Sqrt.java907.00 B2014-05-09 03:48
StringToInteger_atoi.java1.46 kB2014-05-09 03:48
Sum3.java1.76 kB2014-05-09 03:48
TwoSum.java2.33 kB2014-05-09 03:48
UniqueBinarySearchTrees.java1.01 kB2014-05-09 03:48
UniquePaths.java976.00 B2014-05-09 03:48
UniquePathsII.java1.84 kB2014-05-09 03:48
ValidParentheses.java1.23 kB2014-05-09 03:48
ValidateBinarySearchTree.java1.44 kB2014-05-09 03:48
...
Sponsored links

AddTwoNumbers.java (35.66 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D