vigneshb06
BAN USER
This problem can be solved using stack.
Step1: Insert all the elements in the list1 into a stack say s1
Step2: Insert all the elements in the list2 into a stack say s2
Step3:
int val1,val2,carry=0;
while(!s1.empty() || !s2.empty())
{
val1=0;
val2=0;
if(!s1.empty())
{
val1=s1.top();
s1.pop();
}
if(!s2.empty())
{
val2=s2.top();
s2.pop();
}
if(val1+val2+carry> 10)
{
carry=1;
insertintoll((val1+val2+carry)-10);
}
else
{
carry=0;
insertintoll((val1+val2+carry));
}
}
Note: insertintoll() function inserts the value to the front of the new linked list
We can put each element in a tree say BST and then print the tree in inorder or store the elements into an another array inorder.
- vigneshb06 June 16, 2013no need to use extra space here just one integer variable is enough
- vigneshb06 July 18, 20121. Hey guys use two for loop and compute the count for each item and check whether count%2==0 if the condition gets satisfied then print the result
Time Complexity: O(N2) Space Complexity: O(1)
2. Sort the list and use two pointers to check the count and check the same condition as i have mentioned above and if that gets satissfied print the result
Time Complexity: O(N) Space Complexity : O(1)
1. We can use one stack. initialise i=0 and flag=0 stack and queue
- vigneshb06 August 10, 20132. if(flag==0)
insert 2^i elements and elements from stack into the queue and set flag=~flag
else
insert 2^i elements into the stack and remove element one by one and insert into the tree..set flag=~flag
3. do the above step until DLL is empty