Created
January 9, 2014 06:37
-
-
Save non-static/8330305 to your computer and use it in GitHub Desktop.
http://oj.leetcode.com/problems/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
This file contains hidden or 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
| // AddTwoNumbers.cpp : Defines the entry point for the console application. | |
| // | |
| #include "stdafx.h" | |
| /** | |
| * Definition for singly-linked list. | |
| * struct ListNode { | |
| * int val; | |
| * ListNode *next; | |
| * ListNode(int x) : val(x), next(NULL) {} | |
| * }; | |
| */ | |
| struct ListNode { | |
| int val; | |
| ListNode *next; | |
| ListNode(int x) : val(x), next(NULL) {} | |
| }; | |
| class Solution | |
| { | |
| 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; | |
| ListNode *head = NULL; | |
| ListNode *p1 = l1; | |
| ListNode *p2 = l2; | |
| ListNode *p, *q; | |
| int carry = 0; | |
| while (p1 != NULL && p2 != NULL) | |
| { | |
| int sum = p1->val + p2->val + carry; | |
| carry = sum / 10; | |
| sum = sum % 10; | |
| p = new ListNode(sum); | |
| if (head == NULL) | |
| { | |
| head = p; | |
| q = p; | |
| } | |
| else | |
| { | |
| q->next = p; | |
| q = p; | |
| } | |
| p1 = p1->next; | |
| p2 = p2->next; | |
| } | |
| while (p1 != NULL) | |
| { | |
| int sum = p1->val + carry; | |
| carry = sum / 10; | |
| sum = sum % 10; | |
| p = new ListNode(sum); | |
| if (head == NULL) | |
| { | |
| head = p; | |
| q = p; | |
| } | |
| else | |
| { | |
| q->next = p; | |
| q = p; | |
| } | |
| p1 = p1->next; | |
| } | |
| while (p2 != NULL) | |
| { | |
| int sum = p2->val + carry; | |
| carry = sum / 10; | |
| sum = sum % 10; | |
| p = new ListNode(sum); | |
| if (head == NULL) | |
| { | |
| head = p; | |
| q = p; | |
| } | |
| else | |
| { | |
| q->next = p; | |
| q = p; | |
| } | |
| p2 = p2->next; | |
| } | |
| if (carry == 1) | |
| { | |
| p = new ListNode(carry); | |
| q->next = p; | |
| } | |
| return head; | |
| } | |
| ListNode *createList(int input[], int length) | |
| { | |
| ListNode *head = NULL; | |
| ListNode *p, *q; | |
| for (int i = 0; i < length; i++) | |
| { | |
| p = new ListNode(input[i]); | |
| if (i == 0) | |
| { | |
| head = p; | |
| q = p; | |
| } | |
| else | |
| { | |
| q->next = p; | |
| q = p; | |
| } | |
| } | |
| return head; | |
| } | |
| }; | |
| int _tmain(int argc, _TCHAR* argv[]) | |
| { | |
| Solution s; | |
| int inputArray1[] = { 0 }; | |
| int inputArray2[] = { 0 }; | |
| ListNode *input1 = s.createList(inputArray1, 1); | |
| ListNode *input2 = s.createList(inputArray2, 1); | |
| ListNode *result = s.addTwoNumbers(input1, input2); | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment