Write a Java program to Merge a linked list into another linked list at alternate positions


The Java code provided demonstrates how to merge two linked lists by alternating their nodes.

  • The code defines a class named Merge_LinkedList with an inner class Node representing a node in a linked list. Each node contains a String data value and a reference to the next node.
  • The Merge_LinkedList class has a head member variable which represents the head of the linked list.
  • The push method is used to add new nodes to the front of the linked list. It takes a String value as input, creates a new node with the input value, and sets the next reference of the new node to the current head, making it the new head of the linked list.
  • The merge method is used to merge two linked lists. It takes a Merge_LinkedList object q as input, which represents the second linked list to be merged. The method alternates the nodes of the two linked lists, effectively merging them. It uses four pointers, p_curr, q_curr, p_next, and q_next, to keep track of the nodes being merged and their next nodes. It iterates through the linked lists until either one of them becomes null, and at each step, it swaps the next references of the nodes to merge them.
  • The printList method is used to print the data values of the nodes in the linked list.
  • Inside the main method, two Merge_LinkedList objects col1 and col2 are created, representing the first and second linked lists, respectively.
  • Nodes with String values "Red", "Pink", and "Orange" are added to col1 linked list using the push method, and the linked list is printed using the printList method.
  • Nodes with String values "Blue", "Yellow", "Green", "White", and "Black" are added to col2 linked list using the push method, and the linked list is printed using the printList method.
  • The merge method is called on col1 with col2 as an argument, merging the two linked lists.
  • The merged linked list is printed using the printList method on col1.
  • The original col2 linked list remains unchanged, and it is printed using the printList method on col2.

Note: The merging operation demonstrated in this code alternates the nodes of the two linked lists, effectively merging them. However, this approach assumes that the two linked lists are of the same length. If the linked lists have different lengths, the merging result may not be as expected. Additional handling may be required to properly merge linked lists of different lengths.

Source Code

class Merge_LinkedList
{
	Node head;
	class Node
	{
		String data;
		Node next;
		Node(String d)
		{
			data = d; next = null;
		}
	}
	void push(String new_data)
	{
		Node new_node = new Node(new_data);
		new_node.next = head;
		head = new_node;
	}
 
	void merge(Merge_LinkedList q)
	{
		Node p_curr = head, q_curr = q.head;
		Node p_next, q_next;
 
		while (p_curr != null && q_curr != null)
		{
 
			p_next = p_curr.next;
			q_next = q_curr.next;
 
			q_curr.next = p_next;
			p_curr.next = q_curr;
 
			p_curr = p_next;
			q_curr = q_next;
		}
		q.head = q_curr;
	}
 
	void printList()
	{
		Node temp = head;
		while (temp != null)
		{
			System.out.print(temp.data+" ");
			temp = temp.next;
		}
		System.out.println();
	}
 
	public static void main(String args[])
	{
		Merge_LinkedList col1 = new Merge_LinkedList();
		Merge_LinkedList col2 = new Merge_LinkedList();
		col1.push("Red");
		col1.push("Pink");
		col1.push("Orange");
 
		System.out.println("First Linked List..");
		col1.printList();
 
		col2.push("Blue");
		col2.push("Yellow");
		col2.push("Green");
		col2.push("White");
		col2.push("Black");
 
		System.out.println("Second Linked List..");
		col1.merge(col2);
 
		System.out.println("Modified First Linked List..");
		col1.printList();
 
		System.out.println("Modified Second Linked List..");
		col2.printList();
	}
}

Output

First Linked List..
Orange Pink Red
Second Linked List..
Modified First Linked List..
Orange Black Pink White Red Green
Modified Second Linked List..
Yellow Blue

Example Programs