Javatpoint Logo
Javatpoint Logo

Merge Two Sorted Linked Lists

This article will provide an overview and Python implementation of the merge two sorted linked lists algorithm.

Linked lists are fundamental data structures used in computer science and programming. They provide an efficient way to store and organize data non-contiguously. Linked lists consist of nodes containing data and a reference or pointer to the next node in the list. This allows for efficient insertion and removal of nodes from any position in the list.

One common application of linked lists is maintaining sorted data. Given two linked lists that are each sorted, it is often necessary to merge them into a single sorted linked list efficiently. This merged list contains all the data from both input lists in sorted order.

The algorithm to merge two sorted linked lists is an important one frequently appearing in coding interviews and programming exercises. It demonstrates core linked list manipulation and combines concepts such as iteration, pointer manipulation, and sorting algorithm fundamentals.

Merge Two Sorted Linked Lists

Algorithm and Python Implementation

  1. Initialize two pointer variables, p1 and p2, that point to the head nodes of the two linked lists.
  2. Initialize a pointer variable p3 that will eventually point to the head node of the merged linked list.
  3. Initialize a pointer variable beforehand to keep track of the node before p3.
  4. Compare the data values in the nodes p1 and p2 point to.
  5. If p1's data is smaller, set prev to p1 and move p1 to the next node.
  6. Else if p2's data is smaller, set prev to p2 and move p2 to the next node.
  7. Else if the data is equal, set prev to p1, move p1 to the next node, set prev.next to p2, and move p2 to the next node.
  8. Update p3 to point to the node prev points to once prev is updated. This sets up p3 to be the head node.
  9. Repeat steps 4-8 until we reach the end of one or both lists.
  10. If p1 reaches the end first, append the remaining p2 list to the end of the p3 list. If p2 reaches the end first, append the p1 list.
  11. Return the head node p3.

This algorithm iterates through the two lists, comparing data at each iteration and linking nodes together in sorted order to build the merged list. The time complexity is O(n), where n is the number of nodes in both lists. This is an efficient way to merge two sorted lists.

Below is an example implementation in Python:

Python Program


Merge Two Sorted Linked Lists

Explanation

  1. Defines a Node class to store data and pointer to the next node.
  2. Defines a LinkedList class with head node, append and printList methods.
  3. The append method creates a new node and appends it at the end of the list.
  4. printList prints the data in all nodes of the list.
  5. mergeLists function takes head nodes of two lists as parameters.
  6. Creates a dummy node to build the merged list.
  7. Initializes tail to dummy node to keep track of end of merged list.
  8. Initializes p1 and p2 pointers to head of each list.
  9. Loops until either list is empty.
  10. Compares data at p1 and p2. Appends smaller data node to tail.
  11. Advances p1 or p2 pointer accordingly.
  12. Advances tail to the newly appended node.
  13. After the loop, appends any remaining nodes from p1 or p2.
  14. Returns the next node after the dummy, which is the head of the merged list.
  15. Test cases create two sample lists, and merge them using merge lists.
  16. Prints the merged list data using printList.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA