Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. It is not allowed to modify the lists. Also, not allowed to use explicit extra space (Hint: Use Recursion).
Example
Input: First List: 5->6->3 // represents number 563 Second List: 8->4->2 // represents number 842 Output Resultant list: 1->4->0->5 // represents number 1405
Following are the steps.
1) Calculate sizes of given two linked lists.
2) If sizes are same, then calculate sum using recursion. Hold all nodes in recursion call stack till the rightmost node, calculate sum of rightmost nodes and forward carry to left side.
3) If size is not same, then follow below steps:
….a) Calculate difference of sizes of two linked lists. Let the difference be diff
….b) Move diff nodes ahead in the bigger linked list. Now use step 2 to calculate sum of smaller list and right sub-list (of same size) of larger list. Also, store the carry of this sum.
….c) Calculate sum of the carry (calculated in previous step) with the remaining left sub-list of larger list. Nodes of this sum are added at the beginning of sum list obtained previous step.
private int carry = 0; public ListNode addReversedLinkedList(ListNode a, ListNode b) { if(a==null) { return b; } else if(b==null) { return a; } int m = this.getListSize(a); int n = this.getListSize(b); ListNode result; if(m==n) { result = this.addListWithSameSize(a, b); } else { int diff = Math.abs(m-n); ListNode list1 = m>n?a:b; // make sure the first list is larger ListNode list2 = m>n?b:a; ListNode cur = list1; while(diff-->0) cur = cur.next; result = this.addListWithSameSize(cur, list2); result = this.addRemainingList(list1, cur, result); } if(this.carry>0) { ListNode newRes = new ListNode(0); newRes.val = this.carry; newRes.next = result; return newRes; } return result; } private int getListSize(ListNode n) { int size = 0; while(n!= null) { n = n.next; size++; } return size; } private ListNode addListWithSameSize(ListNode a, ListNode b) { if(a==null || b==null) return null; ListNode result = new ListNode(0); result.next = addListWithSameSize(a.next, b.next); int sum = a.val+b.val+this.carry; this.carry = sum/10; result.val = sum%10; return result; } private ListNode addRemainingList(ListNode list1, ListNode cur, ListNode result) { if(list1==cur) return result; ListNode next = addRemainingList(list1.next, cur, result); int sum = list1.val + this.carry; this.carry = sum/10; ListNode ret = new ListNode(0); ret.val = sum%10; ret.next = next; return ret; }
Reference:
相关推荐
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 ...
lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may ...
371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...
2 sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。 文件名 文件描述 链接 complexitypython.txt Python的一些常规操作的复杂度统计 题目清单 Array(数组) ID Difficulty Title Java Python 1 Easy 两数之和 ...
Median of Two Sorted Arrays H-Index II 暴力枚举法 Subsets Subsets II Permutations Permutations II Combinations Letter Combinations of a Phone Number 广度优先搜索 Word Ladder Word Ladder II Surrounded ...
10.2 Linked lists 236 10.3 Implementing pointers and objects 241 10.4 Representing rooted trees 246 11 Hash Tables 253 11.1 Direct-address tables 254 11.2 Hash tables 256 11.3 Hash functions 262 11.4 ...
A soft page fault is resolved from one of the modified, standby, free or zero page transition lists. Paging is represented by a number of counters including page faults/sec, page input/sec and page ...
2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您的关注! 帮助文档 帮助文档存放在...
2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您的关注! 帮助文档 帮助文档存放在...
LeetCode 原创文章每周最少两篇,后续最新文章会在首发,视频首发,大家可以加我进交流群,技术交流或提意见都可以,欢迎Star!...Merge Two Sorted Lists 83 * Remove Duplicates from Sorted Lis