educative.io

Merge Two sorted linked list

I have one query in Merge Two Sorted Linked list .
Here in this solution we are assigning sorted linked list to new_head/s variable.
We have not created separate linked list for new_head.Then how its getting stored as a linked list .Is it just because of pointers we create using s.next ?

Hello Kavitha,

Yes, at the start of the code we store the smallest element from the head of the two sorted linked lists in s and point the new_head at it. Then we start the merging process and create a new linked list (with head at new_head) using s.next. After the linked lists are merged, the new_head is still pointing at the start of the linked list i.e., the smallest element.