Created
July 10, 2013 09:14
-
-
Save luoxiaoxun/5964775 to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
C++: | |
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
if(l1==NULL&&l2==NULL) return NULL; | |
ListNode *head=NULL; | |
ListNode *pre=NULL; | |
int flag=0; | |
while(l1&&l2){ | |
ListNode *cur=new ListNode((l1->val+l2->val+flag)%10); | |
flag=(l1->val+l2->val+flag)/10; | |
if(head==NULL) head=cur; | |
else pre->next=cur; | |
pre=cur; | |
l1=l1->next; | |
l2=l2->next; | |
} | |
while(l1){ | |
ListNode *cur=new ListNode((l1->val+flag)%10); | |
flag=(l1->val+flag)/10; | |
if(head==NULL) head=cur; | |
else pre->next=cur; | |
pre=cur; | |
l1=l1->next; | |
} | |
while(l2){ | |
ListNode *cur=new ListNode((l2->val+flag)%10); | |
flag=(l2->val+flag)/10; | |
if(head==NULL) head=cur; | |
else pre->next=cur; | |
pre=cur; | |
l2=l2->next; | |
} | |
if(flag){ | |
ListNode *cur=new ListNode(flag); | |
pre->next=cur; | |
pre=cur; | |
} | |
return head; | |
} | |
}; | |
Java: | |
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } | |
*/ | |
public class Solution { | |
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
if(l1==null&&l2==null) return null; | |
ListNode head=null; | |
ListNode pre=null; | |
int flag=0; | |
while(l1!=null&&l2!=null){ | |
ListNode cur=new ListNode((l1.val+l2.val+flag)%10); | |
flag=(l1.val+l2.val+flag)/10; | |
if(head==null) head=cur; | |
else pre.next=cur; | |
pre=cur; | |
l1=l1.next; | |
l2=l2.next; | |
} | |
while(l1!=null){ | |
ListNode cur=new ListNode((l1.val+flag)%10); | |
flag=(l1.val+flag)/10; | |
if(head==null) head=cur; | |
else pre.next=cur; | |
pre=cur; | |
l1=l1.next; | |
} | |
while(l2!=null){ | |
ListNode cur=new ListNode((l2.val+flag)%10); | |
flag=(l2.val+flag)/10; | |
if(head==null) head=cur; | |
else pre.next=cur; | |
pre=cur; | |
l2=l2.next; | |
} | |
if(flag>0){ | |
ListNode cur=new ListNode(flag); | |
pre.next=cur; | |
pre=cur; | |
} | |
return head; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment