Guys I was tried to merge two sorted Single Linked List in sorted order. In SO, I found a recursive approach to do that. I was tried hard to understand the code, but wasn't able to fully understand! Are anyone there, who can help me to figure it out clearly. Thanks in advance!
Here is the code snippets:
Node MergeLists(Node list1, Node list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;
if (list1.data < list2.data) {
list1.next = MergeLists(list1.next, list2);
return list1;
} else {
list2.next = MergeLists(list2.next, list1);
return list2;
}
}
Link: Interview: Merging two Sorted Singly Linked List
SORRY, for wasting your time! :(