Merge odd and even positioned nodes of two Linked Lists alternately
Last Updated :
19 Jan, 2022
Given two linked lists L1 and L2, the task is to print a new list obtained by merging odd position nodes of L1 with the even positioned nodes of L2 alternately.
Examples:
Input: L1 = 8->5->3->2->10->NULL, L2 = 11->13->1->6->9->NULL
Output: 8->13->3->6->10->NULL
Explanation:
The odd positioned nodes of L1 are {8, 3, 10} and the even positioned nodes L2 are {13, 6}.
Merging them alternately generates the linked list 8->13->3->6->10->NULL
Input: L1 = 1->5->10->12->13->19->6->NULL, L2 = 2->7->9->NULL
Output: 1->7->10->13->6->NULL
Explanation:
The odd positioned nodes of L1 are {1, 10, 13, 6} and the even positioned node of L2 is {7}.
Merging them alternately generates the linked list 1->7->10->13->6->NULL
Approach: Follow the steps below to solve the problem:
- Start traversing from the first node of L1 and for the second node of L2 simultaneously.
- Add the first node of L1 to the resultant list and connect it with the second node of L2 and move to the next odd node in L1. Similarly, add connect the second node of L2 to the current odd node of L1 and move to the next even node in L2.
- Repeat the above step until the end of one of the lists is reached.
- Traverse the other list and keep adding the required nodes from that list to the resultant list.
- Finally, print the resultant list.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Structure of a Node
class node {
public:
int data;
node* next;
node(int d)
{
data = d;
next = NULL;
}
};
// Function to insert the node
// at the head of the linkedlist
void insert_at_head(node*& head, int data)
{
node* n = new node(data);
n->next = head;
head = n;
return;
}
// Function to print the linked list
void print(node* head)
{
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
return;
}
// Function to merge the odd and
// even positioned nodes of two
// given linked lists alternately
node* merge_alternate(node* head1, node* head2)
{
// Traverse from the second
// node of second linked list
if (head2)
head2 = head2->next;
// Stores the head of
// the resultant list
node* head3 = NULL;
// Stores the current node
node* cur = NULL;
// Store the first node of
// first list in the result
if (head1)
head3 = head1;
// Otherwise
else
head3 = head2;
// Traverse until end of a
// one of the list is reached
while (head1 != NULL && head2 != NULL) {
// If there is a previous node then
// connect that with the current node
if (cur)
cur->next = head1;
// Update the current node
cur = head1;
// If next odd node exists
if (head1->next != NULL)
// Store the next odd node
head1 = head1->next->next;
// Otherwise
else
// Reach end of list
head1 = NULL;
// Connect the first node
// with the second node
cur->next = head2;
// Update the current node
cur = head2;
// If next even node exists
if (head2->next != NULL)
// Store the next even node
head2 = head2->next->next;
// Otherwise
else
// Reach the end of the list
head2 = NULL;
}
// If end of the second
// list has been reached
while (head1 != NULL) {
// Connect with the
// previous node
if (cur)
cur->next = head1;
// Update the current node
cur = head1;
// If next odd node exists
if (head1->next != NULL)
// Store the next odd node
head1 = head1->next->next;
// Otherwise
else
// Reach end of list
head1 = NULL;
}
// If end of second list
// has been reached
while (head2 != NULL) {
// Connect with the
// previous node
if (cur)
cur->next = head2;
// Update the current node
cur = head2;
// If next even node exists
if (head2->next != NULL)
// Store the next odd node
head2 = head2->next->next;
// Otherwise
else
// Reach end of list
head2 = NULL;
}
// End of the resultant list
if (cur)
cur->next = NULL;
// Returning the head of
// the resultant node
return head3;
}
// Driver Code
int main()
{
node *head1 = NULL, *head2 = NULL;
// Create linked list
insert_at_head(head1, 6);
insert_at_head(head1, 19);
insert_at_head(head1, 13);
insert_at_head(head1, 12);
insert_at_head(head1, 10);
insert_at_head(head1, 5);
insert_at_head(head1, 1);
insert_at_head(head2, 9);
insert_at_head(head2, 7);
insert_at_head(head2, 2);
// Merging the linked lists
head1 = merge_alternate(head1, head2);
print(head1);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Structure of a Node
static class node
{
public int data;
node next;
node(int d)
{
data = d;
next = null;
}
};
// Function to insert the node
// at the head of the linkedlist
static node insert_at_head(node head,
int data)
{
node n = new node(data);
n.next = head;
head = n;
return head;
}
// Function to print the linked list
static void print(node head)
{
while (head != null)
{
System.out.print(head.data + " ");
head = head.next;
}
System.out.println();
return;
}
// Function to merge the odd and
// even positioned nodes of two
// given linked lists alternately
static node merge_alternate(node head1,
node head2)
{
// Traverse from the second
// node of second linked list
if (head2 != null)
head2 = head2.next;
// Stores the head of
// the resultant list
node head3 = null;
// Stores the current node
node cur = null;
// Store the first node of
// first list in the result
if (head1 != null)
head3 = head1;
// Otherwise
else
head3 = head2;
// Traverse until end of a
// one of the list is reached
while (head1 != null && head2 != null)
{
// If there is a previous node then
// connect that with the current node
if (cur != null)
cur.next = head1;
// Update the current node
cur = head1;
// If next odd node exists
if (head1.next != null)
// Store the next odd node
head1 = head1.next.next;
// Otherwise
else
// Reach end of list
head1 = null;
// Connect the first node
// with the second node
cur.next = head2;
// Update the current node
cur = head2;
// If next even node exists
if (head2.next != null)
// Store the next even node
head2 = head2.next.next;
// Otherwise
else
// Reach the end of the list
head2 = null;
}
// If end of the second
// list has been reached
while (head1 != null)
{
// Connect with the
// previous node
if (cur != null)
cur.next = head1;
// Update the current node
cur = head1;
// If next odd node exists
if (head1.next != null)
// Store the next odd node
head1 = head1.next.next;
// Otherwise
else
// Reach end of list
head1 = null;
}
// If end of second list
// has been reached
while (head2 != null)
{
// Connect with the
// previous node
if (cur != null)
cur.next = head2;
// Update the current node
cur = head2;
// If next even node exists
if (head2.next != null)
// Store the next odd node
head2 = head2.next.next;
// Otherwise
else
// Reach end of list
head2 = null;
}
// End of the resultant list
if (cur != null)
cur.next = null;
// Returning the head of
// the resultant node
return head3;
}
// Driver Code
public static void main(String[] args)
{
node head1 = null, head2 = null;
// Create linked list
head1 = insert_at_head(head1, 6);
head1 = insert_at_head(head1, 19);
head1 = insert_at_head(head1, 13);
head1 = insert_at_head(head1, 12);
head1 = insert_at_head(head1, 10);
head1 = insert_at_head(head1, 5);
head1 = insert_at_head(head1, 1);
head2 = insert_at_head(head2, 9);
head2 = insert_at_head(head2, 7);
head2 = insert_at_head(head2, 2);
// Merging the linked lists
head1 = merge_alternate(head1, head2);
print(head1);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program to implement
# the above approach
# Structure of a Node
class node:
def __init__(self, d):
self.data = d;
self.next = None;
# Function to insert the node
# at the head of the linkedlist
def insert_at_head(head, data):
n = node(data);
n.next = head;
head = n;
return head;
# Function to print the linked list
def printx(head):
while (head != None):
print(head.data, end = ' ')
head = head.next;
print()
return;
# Function to merge the odd and
# even positioned nodes of two
# given linked lists alternately
def merge_alternate(head1, head2):
# Traverse from the second
# node of second linked list
if (head2):
head2 = head2.next;
# Stores the head of
# the resultant list
head3 = None;
# Stores the current node
cur = None;
# Store the first node of
# first list in the result
if (head1):
head3 = head1;
# Otherwise
else:
head3 = head2;
# Traverse until end of a
# one of the list is reached
while (head1 != None and head2 != None):
# If there is a previous node then
# connect that with the current node
if (cur):
cur.next = head1;
# Update the current node
cur = head1;
# If next odd node exists
if (head1.next != None):
# Store the next odd node
head1 = head1.next.next;
# Otherwise
else:
# Reach end of list
head1 = None;
# Connect the first node
# with the second node
cur.next = head2;
# Update the current node
cur = head2;
# If next even node exists
if (head2.next != None):
# Store the next even node
head2 = head2.next.next;
# Otherwise
else:
# Reach the end of the list
head2 = None;
# If end of the second
# list has been reached
while (head1 != None):
# Connect with the
# previous node
if (cur):
cur.next = head1;
# Update the current node
cur = head1;
# If next odd node exists
if (head1.next != None):
# Store the next odd node
head1 = head1.next.next;
# Otherwise
else:
# Reach end of list
head1 = None;
# If end of second list
# has been reached
while (head2 != None):
# Connect with the
# previous node
if (cur):
cur.next = head2;
# Update the current node
cur = head2;
# If next even node exists
if (head2.next != None):
# Store the next odd node
head2 = head2.next.next;
# Otherwise
else:
# Reach end of list
head2 = None;
# End of the resultant list
if (cur):
cur.next = None;
# Returning the head of
# the resultant node
return head3;
# Driver Code
if __name__=='__main__':
head1 = None
head2 = None;
# Create linked list
head1 = insert_at_head(head1, 6);
head1 = insert_at_head(head1, 19);
head1 = insert_at_head(head1, 13);
head1 = insert_at_head(head1, 12);
head1 = insert_at_head(head1, 10);
head1 = insert_at_head(head1, 5);
head1 = insert_at_head(head1, 1);
head2 = insert_at_head(head2, 9);
head2 = insert_at_head(head2, 7);
head2 = insert_at_head(head2, 2)
# Merging the linked lists
head1 = merge_alternate(head1, head2);
printx(head1);
# This code is contributed by rutvik_56
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Structure of a Node
class node
{
public int data;
public node next;
public node(int d)
{
data = d;
next = null;
}
};
// Function to insert the node
// at the head of the linkedlist
static node insert_at_head(node head,
int data)
{
node n = new node(data);
n.next = head;
head = n;
return head;
}
// Function to print the linked list
static void print(node head)
{
while (head != null)
{
Console.Write(head.data + " ");
head = head.next;
}
Console.WriteLine();
return;
}
// Function to merge the odd and
// even positioned nodes of two
// given linked lists alternately
static node merge_alternate(node head1,
node head2)
{
// Traverse from the second
// node of second linked list
if (head2 != null)
head2 = head2.next;
// Stores the head of
// the resultant list
node head3 = null;
// Stores the current node
node cur = null;
// Store the first node of
// first list in the result
if (head1 != null)
head3 = head1;
// Otherwise
else
head3 = head2;
// Traverse until end of a
// one of the list is reached
while (head1 != null &&
head2 != null)
{
// If there is a previous
// node then connect that
// with the current node
if (cur != null)
cur.next = head1;
// Update the current node
cur = head1;
// If next odd node exists
if (head1.next != null)
// Store the next odd node
head1 = head1.next.next;
// Otherwise
else
// Reach end of list
head1 = null;
// Connect the first node
// with the second node
cur.next = head2;
// Update the current node
cur = head2;
// If next even node exists
if (head2.next != null)
// Store the next even node
head2 = head2.next.next;
// Otherwise
else
// Reach the end of the list
head2 = null;
}
// If end of the second
// list has been reached
while (head1 != null)
{
// Connect with the
// previous node
if (cur != null)
cur.next = head1;
// Update the current node
cur = head1;
// If next odd node exists
if (head1.next != null)
// Store the next odd node
head1 = head1.next.next;
// Otherwise
else
// Reach end of list
head1 = null;
}
// If end of second list
// has been reached
while (head2 != null)
{
// Connect with the
// previous node
if (cur != null)
cur.next = head2;
// Update the current node
cur = head2;
// If next even node exists
if (head2.next != null)
// Store the next odd node
head2 = head2.next.next;
// Otherwise
else
// Reach end of list
head2 = null;
}
// End of the resultant list
if (cur != null)
cur.next = null;
// Returning the head of
// the resultant node
return head3;
}
// Driver Code
public static void Main(String[] args)
{
node head1 = null, head2 = null;
// Create linked list
head1 = insert_at_head(head1, 6);
head1 = insert_at_head(head1, 19);
head1 = insert_at_head(head1, 13);
head1 = insert_at_head(head1, 12);
head1 = insert_at_head(head1, 10);
head1 = insert_at_head(head1, 5);
head1 = insert_at_head(head1, 1);
head2 = insert_at_head(head2, 9);
head2 = insert_at_head(head2, 7);
head2 = insert_at_head(head2, 2);
// Merging the linked lists
head1 = merge_alternate(head1, head2);
print(head1);
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// javascript program to implement
// the above approach
// Structure of a Node
class node {
constructor(d) {
this.data = d;
this.next = null;
}
}
// Function to insert the node
// at the head of the linkedlist
function insert_at_head( head , data) {
n = new node(data);
n.next = head;
head = n;
return head;
}
// Function to print the linked list
function print( head) {
while (head != null) {
document.write(head.data + " ");
head = head.next;
}
document.write();
return;
}
// Function to merge the odd and
// even positioned nodes of two
// given linked lists alternately
function merge_alternate( head1, head2) {
// Traverse from the second
// node of second linked list
if (head2 != null)
head2 = head2.next;
// Stores the head of
// the resultant list
head3 = null;
// Stores the current node
cur = null;
// Store the first node of
// first list in the result
if (head1 != null)
head3 = head1;
// Otherwise
else
head3 = head2;
// Traverse until end of a
// one of the list is reached
while (head1 != null && head2 != null) {
// If there is a previous node then
// connect that with the current node
if (cur != null)
cur.next = head1;
// Update the current node
cur = head1;
// If next odd node exists
if (head1.next != null)
// Store the next odd node
head1 = head1.next.next;
// Otherwise
else
// Reach end of list
head1 = null;
// Connect the first node
// with the second node
cur.next = head2;
// Update the current node
cur = head2;
// If next even node exists
if (head2.next != null)
// Store the next even node
head2 = head2.next.next;
// Otherwise
else
// Reach the end of the list
head2 = null;
}
// If end of the second
// list has been reached
while (head1 != null) {
// Connect with the
// previous node
if (cur != null)
cur.next = head1;
// Update the current node
cur = head1;
// If next odd node exists
if (head1.next != null)
// Store the next odd node
head1 = head1.next.next;
// Otherwise
else
// Reach end of list
head1 = null;
}
// If end of second list
// has been reached
while (head2 != null) {
// Connect with the
// previous node
if (cur != null)
cur.next = head2;
// Update the current node
cur = head2;
// If next even node exists
if (head2.next != null)
// Store the next odd node
head2 = head2.next.next;
// Otherwise
else
// Reach end of list
head2 = null;
}
// End of the resultant list
if (cur != null)
cur.next = null;
// Returning the head of
// the resultant node
return head3;
}
// Driver Code
head1 = null, head2 = null;
// Create linked list
head1 = insert_at_head(head1, 6);
head1 = insert_at_head(head1, 19);
head1 = insert_at_head(head1, 13);
head1 = insert_at_head(head1, 12);
head1 = insert_at_head(head1, 10);
head1 = insert_at_head(head1, 5);
head1 = insert_at_head(head1, 1);
head2 = insert_at_head(head2, 9);
head2 = insert_at_head(head2, 7);
head2 = insert_at_head(head2, 2);
// Merging the linked lists
head1 = merge_alternate(head1, head2);
print(head1);
// This code is contributed by umadevi9616
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Alternate Odd and Even Nodes in a Singly Linked List
Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where
15+ min read
Delete all odd or even positioned nodes from Circular Linked List
Delete all odd or even position nodes from circular linked list Given a Singly Circular Linked List, starting from the first node delete all odd position nodes in it. Note: Linked list is considered to have 1-based indexing. That is, first element in the linked list is at position 1. Examples:  In
15+ min read
Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
Given a linked list. The task is to segregate its even and odd position nodes in such a way that odd position nodes appear before even positioned nodes all the even positioned nodes must be in reverse order. Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL Output : 1 -> 3 -
11 min read
Rearrange a linked list such that all even and odd positioned nodes are together
Given a linked list, the task is to rearrange the list in such a way that all odd positioned nodes are together and all even positioned nodes are together.Examples: Input: 1 -> 2 -> 3 -> 4Output: 1 -> 3 -> 2 -> 4Input: 10 -> 22 -> 30 -> 43 -> 56 -> 70Output: 10 ->
13 min read
Reverse the order of all nodes at even position in given Linked List
Given a linked list A[] of N integers, the task is to reverse the order of all integers at an even position. Examples: Input: A[] = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLOutput: 1 6 3 4 5 2Explanation: Nodes at even position in the given linked list are 2, 4 and 6. So, after reversing
10 min read
Print nodes of linked list at given indexes
Given head of two singly linked lists, first one is sorted and the other one is unsorted. The task is to print the elements of the second linked list according to the position pointed out by the data in the first linked list. For example, if the first linked list is 1->2->5, then you have to p
15+ min read
Segregate even and odd nodes in a Linked List
Given a Linked List of integers, The task is to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, preserve the order of even and odd numbers.Examples: Input: 17->15->8->12->10->5->4->1->7->6->NULLOutpu
15+ min read
Delete alternate nodes of a Linked List
Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm
14 min read
Merge a linked list into another linked list at alternate positions
Given two singly linked lists, The task is to insert nodes of the second list into the first list at alternate positions of the first list and leave the remaining nodes of the second list if it is longer.Example:Input: head1: 1->2->3 , head2: 4->5->6->7->8Output: head1: 1->4-
8 min read
Javascript Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order
Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where
7 min read