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 两数之和 ...
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公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您的关注! 帮助文档 帮助文档存放在...
- **Power of Two**:判断一个整数是否为2的幂次方。 - **Number of 1 Bits**:计算一个整数中1的个数。 3. **树(Tree)**: - **Depth of Binary Tree**:计算二叉树的深度。 - **Construct Binary Tree**:...
2. **两数之和(Two Sum)** - **问题描述**:给定一个整数数组nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,并返回它们的数组下标。 - **解决方案**: - 使用哈希表存储已访问过的数值...
2. **链表**:链表数据结构在LeetCode中也占据重要地位,如"删除链表中的元素"(Remove Element)和"两链表相交"(Intersection of Two Linked Lists)。Swift虽然没有内置链表,但开发者可以自定义节点结构体来模拟...
6. **链表操作**:链表题目考察了指针操作和逻辑思维,如"Reverse Linked List"(反转链表)和"Merge Two Sorted Lists"(合并两个排序链表)。 7. **哈希表**:哈希表的高效查找和存储特性使得它在解决许多问题时...
23. Merge K Sorted Linked Lists:合并K个排序链表。 24. Copy List with Random Pointer:复制含有随机指针的链表。 四、二叉树 25. Validate Binary Search Tree:验证二叉搜索树的合法性。 26. Maximum Depth ...
例如,“两链表的交点”(Intersection of Two Linked Lists)要求找出两个链表的第一个公共节点。 3. **字符串(Strings)**:处理字符串的拼接、查找、替换等问题。如“无重复字符的最长子串”(Longest ...
#### 一、两数之和(Two Sum) **问题描述:** 给定一个整数数组 `nums` 和一个整数目标值 `target`,请在该数组中找出和为目标值的那两个整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,...
LeetCode 原创文章每周最少两篇,后续最新文章会在首发,视频首发,大家可以加我进交流群,技术交流或提意见都可以,欢迎Star!...Merge Two Sorted Lists 83 * Remove Duplicates from Sorted Lis
- **判断2的幂(Power of Two)**: 判断一个给定的正整数是否为2的幂。 - **计算1的位数(Number of 1 Bits)**: 计算一个无符号整数的二进制表示中有多少个1位。 ##### 树(Tree) - **二叉树的深度(Depth of Binary ...
3. **链表操作**:如“两链表的交点”(Intersection of Two Linked Lists),需要找到两个链表的第一个公共节点。 4. **树结构**:如“二叉树的最近公共祖先”(Lowest Common Ancestor of a Binary Tree),需要找到...
例如,Reverse Linked List(反转链表)和Intersection of Two Linked Lists(两个链表的交集)等题目,都需要对链表进行操作。 3. 栈与队列:栈具有后进先出(LIFO)特性,队列则遵循先进先出(FIFO)原则。例如,...