You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *l3 = NULL, *t1, *t2, *t3;
int carry = 0, sum = 0;
t1 = l1;
t2 = l2;
while (t1 != NULL && t2 != NULL) {
if (l3 == NULL) {
l3 = (struct ListNode *)malloc(sizeof(struct ListNode));
if (l3 == NULL)
return NULL;
l3->next = NULL;
t3 = l3;
} else {
t3->next = (struct ListNode *)malloc(sizeof(struct ListNode));
if (t3->next == NULL)
return NULL;
t3 = t3->next;
t3->next = NULL;
}
sum = t1->val + t2->val + carry;
carry = 0;
if (sum >= 10) {
carry = 1;
sum -= 10;
}
t3->val = sum;
t1 = t1->next;
t2 = t2->next;
}
while (t1 != NULL) {
t3->next = (struct ListNode *)malloc(sizeof(struct ListNode));
if (t3->next == NULL)
return NULL;
t3 = t3->next;
t3->next = NULL;
sum = t1->val + carry;
carry = 0;
if (sum >= 10) {
carry = 1;
sum -= 10;
}
t3->val = sum ;
t1 = t1->next;
}
while (t2 != NULL) {
t3->next = (struct ListNode *)malloc(sizeof(struct ListNode));
if (t3->next == NULL)
return NULL;
t3 = t3->next;
t3->next = NULL;
sum = t2->val + carry;
carry = 0;
if (sum >= 10) {
carry = 1;
sum -= 10;
}
t3->val = sum;
t2 = t2->next;
}
if (carry == 1) {
t3->next = (struct ListNode *)malloc(sizeof(struct ListNode));
if (t3->next == NULL)
return NULL;
t3 = t3->next;
t3->next = NULL;
t3->val = carry;
}
return l3;
}
Solution in Python:
# Definition for singly-linked list.
#class ListNode(object):
# def __init__(self, x):
# self.val = x
#self.next = None
class Solution(object):
def reverseList(self, l1):
prev = None
curr = l1
next = None
while (curr != None):
next = curr.next
curr.next = prev
prev = curr
curr = next
return prev
def printList(self, l1):
while(l1 != None):
print(l1.val)
print(",")
l1 = l1.next
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
carry = 0
l3 = None
head = None
while ((l1 != None) and (l2 != None)):
s = l1.val + l2.val + carry
print(str(s))
carry = 0
if (s > 9) :
carry = 1
s = s % 10
if (head == None):
l3 = ListNode(s)
head = l3
l1 = l1.next
l2 = l2.next
continue
l3.next = ListNode(s)
l3 = l3.next
l1 = l1.next
l2 = l2.next
while (l1 != None):
s = l1.val + carry
carry = 0
if (s > 9):
carry = 1
s = s % 10
tmp = ListNode(s)
if (head == None):
l3 = tmp
head = l3
l1 = l1.next
continue
l3.next = tmp
l3 = tmp
l1 = l1.next
while (l2 != None):
s = l2.val + carry
carry = 0
if (s > 9):
carry = 1
s = s % 10
tmp = ListNode(s)
if (head == None):
l3 = tmp
head = l3
l2 = l2.next
continue
l3.next = tmp
l3 = tmp
l2 = l2.next
if (carry != 0):
tmp = ListNode(carry)
l3.next = tmp
return head
No comments:
Post a Comment