'How can I merge unsorted linked list in java
// How can I make this function? I want to merge two unsorted linked list.
public static LinkedList mergeUnsortedLists(LinkedList list1, LinkedList list2) {
LinkedList list3= new LinkedList();
Node curr_odd = list1.head;
Node curr_even = list2.head;
Node prev = null;
while(curr_odd != null){
prev = curr_odd;
curr_odd = curr_odd.getNext();
}
prev = curr_even;
return list3;
}
Solution 1:[1]
public static LinkedList mergeUnsortedLists(LinkedList list1, LinkedList list2) {
if (lis1.head == null) {
return list2;
}
if (list2.head == null) {
return list1;
}
for (Node crt = list1.head; crt.getNext() != null; crt = crt.getNext());
// Need to link last element in l1 to head element of l2
crt.setNext(list2.head);
return list1;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexandru Placinta |
