Given two sorted Linked Lists, we need to merge them into the third list in sorted order. Complexity – O(n)
Solution :
public Node mergeList(Node a, Node b){
Node result = null;
if(a==null)
return b;
if(b==null)
return a;
if(a.data <= b.data){
result =a;
result.next = mergeList(a.next,b);
}
else
{
result =b;
result.next = mergeList(b.next,a);
}
return result;
}



Working on machines without understanding them ? Then you should be here..
Geographical location should not become a barrier to Share our knowledge.
Puzzles and Interview question are intended to be discussed here.
0 comments:
Post a Comment