Problem Statement:
You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.
My Approach:
1.First I start with a dummy node or temporary node that called temp and a pointer tail to build the merged list.
2.Then I compare the current nodes of list1 and list2.
3.Then I attach the smaller node to tail.next and move that list forward.
4.Then I move tail forward after each attachment.
5.After one list is empty, I attach the remaining nodes from the other list.
6.Then I return temp.next as the head of the merged sorted list.

Top comments (0)