Given 2 Linked Lists containing single digit as data representing number with most significant digit at head and least significant at the tail return the addition of both.
The solution must not reverse the given linked lists and do it in O(n) time complexity and constant space complexity(Recursive solution will take O(n) space because of stack).
For example:
9 -> 3 -> 2 -> 2 ->
+
3 -> 4 -> 8 -> 3 ->
=
1 -> 2 -> 8 -> 0 -> 5 .
Can anyone help me out ?
it is easily possible in doubly linked list, but question is unclear whether it is singly LL or doubly LL.
If singly LL, then you can do this:
Pad the shorter list with zeros to make equal length.
Start from head and and the digits and insert result to answer linked list at head. (Ex: 5->10->7->12)
Now you will have to propagate carry to end.
Reverse the answer linked list.
Answer linked list can be made from any of two given linked list if you cannot modify the lists then I don't think it is possible in O(1) space as answer will take Ω(n).
Need to do this in singly LL.
I think I solved this with only four pointers (even without integer variables).
First, we do some preparation to cope with the difference of the input lengths. Without loss of generality assume that the first list is longer. To do so, simultaneously advance two pointers from each head. When the second pointer reaches the end, the third pointer will be set to the head of the first list. Then advance the first and third pointers simultaneously until the first pointer reaches the end. At this time, the third pointer is saved. When we iterate the second list from its head, the pointer will be freeze until the pointer on the first list reaches the saved third pointer. While the second pointer is freezing, it reads 0 as the digit. In this way, we can synchronize the two pointers on each list.
We use forth and fifth pointers to save the previous node of the beginning of the last 9-run for each list. 9-run means a possibly empty consecutive sequence of 9's on the result of the digit sums (This may not match the final result). When the result of the sum of the current digits is greater or equals to 10 (it carries), the last 9-run is "consumed". The result will be output during the consumption.
The second pointer can replace the fifth pointer while the third pointer is being used. Thus the four pointers are enough.
I understood the part where the third pointer will be after the first pointer reaches the end. Can you please explain me 9 9 9 9 3 2 2 + 3 4 8 3. It will be really helpful if you can explain your approach by this example. Thankyou in advance.
include<bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node *link;
};
void add_at_end(struct node *);
struct node * add(struct node *,struct node *);
void print(struct node *);
int main(){
}
void add_at_end(struct node *head){
} void print(struct node *head){
}
struct node * add(struct node *head,struct node *head1){
}
Output:
4
9 3 2 2
3 4 8 3
1 2 8 0 5