Combine two sorted linked list
Page 1 of 1
Combine two sorted linked list
Recursive:
public class Merge{
public LinkedList output;
public void combine(LinkedListNode l1, LinkedListNode l2){
if(l1==null&&l2==null){
return;
}
else if(l1==null){
while(l2){
output.add(l2);
l2=l2.next;
}
}
else if(l2==null){}
else{
if(l1.value<l2.value){
output.add(l1);
combine(l1.next, l2);
}
else{
output.add(l2);
combine(l1,l2.next);
}
}
return;
}
}
public class Merge{
public LinkedList output;
public void combine(LinkedListNode l1, LinkedListNode l2){
if(l1==null&&l2==null){
return;
}
else if(l1==null){
while(l2){
output.add(l2);
l2=l2.next;
}
}
else if(l2==null){}
else{
if(l1.value<l2.value){
output.add(l1);
combine(l1.next, l2);
}
else{
output.add(l2);
combine(l1,l2.next);
}
}
return;
}
}
Similar topics
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» Convert any BST into doubly linked list in place
» Revert a linked list with a cycle in it. (Thus removing the cycle)
» Two Sorted Array, merge them
» Insert in front of the list
» Convert any BST into doubly linked list in place
» Revert a linked list with a cycle in it. (Thus removing the cycle)
» Two Sorted Array, merge them
» Insert in front of the list
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|