ADD Numbers
You have both numbers represented by a linked list, where each node contains a single digit. reverse
the digits is stored in order, such the 1 's digit is at the head of the list. Write a function that adds the numbers and returns the sum as a linked list.
Example
Given 7->1->6 + 5->9->2
. That's, 617 + 295
.
Return 2->1->9
. That's 912
.
Given 3->1->5
5->9->2
and, return 8->0->8
.
Solution:
It is simple to add, to see a linkedlist again, and then to consider the carry, a carry count, the number of bits to add and count.
Code:
/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int X) {* val = x; * next = NULL; * } * } */ Public classSolution {/** * @paraml1:the First list *@paraml2:the Second list *@return: The sum list of L1 and L2*/ PublicListNode addlists (listnode L1, ListNode L2) {if(L1 = =NULL){ returnL2; } if(L2 = =NULL){ returnL1; } intCount = 0; //int sum = 0;ListNode dummy =NewListNode (0); ListNode Head=dummy; while(L1! =NULL&& L2! =NULL) {Head.next=NewListNode ((l1.val + l2.val + count)% 10); Count= (L1.val + l2.val + count)/10; L1=L1.next; L2=L2.next; Head=Head.next; } while(L1! =NULL) {Head.next=NewListNode ((l1.val + count)% 10); Count= (L1.val + count)/10; //head = Head.next;L1 =L1.next; Head=Head.next; } while(L2! =NULL) {Head.next=NewListNode ((l2.val + count)% 10); Count= (L2.val + count)/10; //head = Head.next;L2 =L2.next; Head=Head.next; } if(Count! = 0) {Head.next=NewListNode (count); } returnDummy.next; }}
View Code
Follow up:
Add Numbers II
You have both numbers represented by a linked list, where each node contains a single digit. forward
the digits is stored in order, such the 1 's digit is at the head of the list. Write a function that adds the numbers and returns the sum as a linked list.
Example
Given 6->1->7 + 2->9->5
. That's, 617 + 295
.
Return 9->1->2
. That's, 912
.
Solution:
In fact is the operation of the positive, to the normal addition, but since we will be upside down, is certainly also ah! We will first reverse, put these all upside down, and then use the first method add, and then the answer reverse back, OK!
Code:
/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int X) {* val = x; * next = NULL; * } * } */ Public classSolution {/** * @paraml1:the First list *@paraml2:the Second list *@return: The sum list of L1 and L2*/ PublicListNode addLists2 (listnode L1, ListNode L2) {if(L1 = =NULL){ returnL2; } if(L2 = =NULL){ returnL1; } L1=reverse (L1); L2=reverse (L2); ListNode Head=Add (L1, L2); returnreverse (head); } PrivateListNode Reverse (listnode head) {if(Head = =NULL){ return NULL; } ListNode Pre=NULL; while(Head! =NULL) {ListNode temp=Head.next; Head.next=Pre; Pre=Head; Head=temp; } returnPre; } Privatelistnode Add (listnode L1, ListNode L2) {if(L1 = =NULL){ returnL2; } if(L2 = =NULL){ returnL1; } intsum = 0; intCount = 0; ListNode Dummy=NewListNode (0); ListNode Curr=dummy; while(L1! =NULL&& L2! =NULL) {sum= L1.val + L2.val +count; Curr.next=NewListNode (sum% 10); Count= SUM/10; Curr=Curr.next; L1=L1.next; L2=L2.next; } while(L1! =NULL) {sum= L1.val +count; Curr.next=NewListNode (sum% 10); Count= SUM/10; Curr=Curr.next; L1=L1.next; } while(L2! =NULL) {sum= L2.val +count; Curr.next=NewListNode (sum% 10); Count= SUM/10; Curr=Curr.next; L2=L2.next; } if(Count! = 0) {Curr.next=NewListNode (count); } returnDummy.next; }}
View Code
[Lintcode] Add Numbers I && II