Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Recursion
  • Practice Recursion
  • MCQs on Recursion
  • Recursion Tutorial
  • Recursive Function
  • Recursion vs Iteration
  • Types of Recursions
  • Tail Recursion
  • Josephus Problem
  • Tower of Hanoi
  • Check Palindrome
Open In App
Next Article:
Reverse alternate K nodes in a Singly Linked List
Next article icon

Recursive approach for alternating split of Linked List

Last Updated : 23 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a linked list, split the linked list into two with alternate nodes.

Examples: 

Input : 1 2 3 4 5 6 7 Output : 1 3 5 7          2 4 6  Input : 1 4 5 6 Output : 1 5          4 6

We have discussed Iterative splitting of linked list.

The idea is to begin from two nodes first and second. Let us call these nodes as ‘a’ and ‘b’. We recurs

Implementation:

C++




// CPP code to split linked list
#include <bits/stdc++.h>
using namespace std;
 
// Node structure
struct Node {
    int data;
    struct Node* next;
};
 
// Function to push nodes
// into linked list
void push(Node** head, int new_data)
{
    Node* new_node = new Node;
    new_node->data = new_data;
    new_node->next = (*head);
    (*head) = new_node;
}
 
// We basically remove link between 'a'
// and its next. Similarly we remove link
// between 'b' and its next. Then we recur
// for remaining lists.
void moveNode(Node* a, Node* b)
{
    if (b == NULL || a == NULL)
        return;
 
    if (a->next != NULL)
        a->next = a->next->next;
 
    if (b->next != NULL)
        b->next = b->next->next;
 
    moveNode(a->next, b->next);
}
 
// function to split linked list
void alternateSplitLinkedList(Node* head, Node** aRef,
                                        Node** bRef)
{
    Node* curr = head;
    *aRef = curr;
    *bRef = curr->next;
    moveNode(*aRef, *bRef);
}
 
void display(Node* head)
{
    Node* curr = head;
    while (curr != NULL) {
        printf("%d ", curr->data);
        curr = curr->next;
    }
}
 
// Driver code
int main()
{
    Node* head = NULL;
    Node *a = NULL, *b = NULL;
 
    push(&head, 7);
    push(&head, 6);
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
 
 
    alternateSplitLinkedList(head, &a, &b);
 
    printf("a : ");
    display(a);
    printf("\nb : ");
    display(b);
 
    return 0;
}
 
 

Java




// Java code to split linked list
class GFG
{
     
// Node structure
static class Node
{
    int data;
    Node next;
};
 
// Function to push nodes
// into linked list
static Node push(Node head, int new_data)
{
    Node new_node = new Node();
    new_node.data = new_data;
    new_node.next = (head);
    (head) = new_node;
    return head;
}
 
static Node a = null, b = null;
 
// We basically remove link between 'a'
// and its next. Similarly we remove link
// between 'b' and its next. Then we recur
// for remaining lists.
static void moveNode(Node a, Node b)
{
    if (b == null || a == null)
        return;
 
    if (a.next != null)
        a.next = a.next.next;
 
    if (b.next != null)
        b.next = b.next.next;
 
    moveNode(a.next, b.next);
}
 
// function to split linked list
static void alternateSplitLinkedList(Node head)
{
    Node curr = head;
    a = curr;
    b = curr.next;
    Node aRef = a, bRef = b;
    moveNode(aRef, bRef);
}
 
static void display(Node head)
{
    Node curr = head;
    while (curr != null)
    {
        System.out.printf("%d ", curr.data);
        curr = curr.next;
    }
}
 
// Driver code
public static void main(String args[])
{
    Node head = null;
    head = push(head, 7);
    head = push(head, 6);
    head = push(head, 5);
    head = push(head, 4);
    head = push(head, 3);
    head = push(head, 2);
    head = push(head, 1);
     
    alternateSplitLinkedList(head);
 
    System.out.printf("a : ");
    display(a);
    System.out.printf("\nb : ");
    display(b);
}
}
 
// This code is contributed by Arnab Kundu
 
 

Python




# Python code to split linked list
 
# Node structure
class Node(object):
    def __init__(self, d):
        self.data = d
        self.next = None
 
# Function to push nodes
# into linked list
def push( head, new_data) :
 
    new_node = Node(0)
    new_node.data = new_data
    new_node.next = (head)
    (head) = new_node
    return head
 
a = None
b = None
 
# We basically remove link between 'a'
# and its next. Similarly we remove link
# between 'b' and its next. Then we recur
# for remaining lists.
def moveNode( a, b) :
    if (b == None or a == None) :
        return
    if (a.next != None) :
        a.next = a.next.next
    if (b.next != None) :
        b.next = b.next.next
 
    moveNode(a.next, b.next)
 
# function to split linked list
def alternateSplitLinkedList(head) :
     
    curr = head
    global a
    global b
    a = curr
    b = curr.next
    aRef = a
    bRef = b
    moveNode(aRef, bRef)
    return head;
 
def display(head) :
 
    curr = head
    while (curr != None) :
     
        print( curr.data,end = " ")
        curr = curr.next
     
# Driver code
head = None
head = push(head, 7)
head = push(head, 6)
head = push(head, 5)
head = push(head, 4)
head = push(head, 3)
head = push(head, 2)
head = push(head, 1)
     
head=alternateSplitLinkedList(head)
 
print("a : ",end="")
display(a)
print("\nb : ",end="")
display(b)
 
# This code is contributed by Arnab Kundu
 
 

C#




// C# code to split linked list
using System;
 
class GFG
{
     
// Node structure
public class Node
{
    public int data;
    public Node next;
};
 
// Function to push nodes
// into linked list
static Node push(Node head, int new_data)
{
    Node new_node = new Node();
    new_node.data = new_data;
    new_node.next = (head);
    (head) = new_node;
    return head;
}
 
static Node a = null, b = null;
 
// We basically remove link between 'a'
// and its next. Similarly we remove link
// between 'b' and its next. Then we recur
// for remaining lists.
static void moveNode(Node a, Node b)
{
    if (b == null || a == null)
        return;
 
    if (a.next != null)
        a.next = a.next.next;
 
    if (b.next != null)
        b.next = b.next.next;
 
    moveNode(a.next, b.next);
}
 
// function to split linked list
static void alternateSplitLinkedList(Node head)
{
    Node curr = head;
    a = curr;
    b = curr.next;
    Node aRef = a, bRef = b;
    moveNode(aRef, bRef);
}
 
static void display(Node head)
{
    Node curr = head;
    while (curr != null)
    {
        Console.Write("{0} ", curr.data);
        curr = curr.next;
    }
}
 
// Driver code
public static void Main(String []args)
{
    Node head = null;
    head = push(head, 7);
    head = push(head, 6);
    head = push(head, 5);
    head = push(head, 4);
    head = push(head, 3);
    head = push(head, 2);
    head = push(head, 1);
     
    alternateSplitLinkedList(head);
 
    Console.Write("a : ");
    display(a);
    Console.Write("\nb : ");
    display(b);
}
}
 
// This code is contributed by PrinciRaj1992
 
 

Javascript




<script>
 
// JavaScript code to split linked list
 
// Node structure
class Node
{
    constructor()
    {
        this.data = 0;
        this.next = null
    }
};
 
// Function to push nodes
// into linked list
function push(head, new_data)
{
    var new_node = new Node();
    new_node.data = new_data;
    new_node.next = (head);
    (head) = new_node;
    return head;
}
 
var a = null, b = null;
 
// We basically remove link between 'a'
// and its next. Similarly we remove link
// between 'b' and its next. Then we recur
// for remaining lists.
function moveNode(a, b)
{
    if (b == null || a == null)
        return;
 
    if (a.next != null)
        a.next = a.next.next;
 
    if (b.next != null)
        b.next = b.next.next;
 
    moveNode(a.next, b.next);
}
 
// function to split linked list
function alternateSplitLinkedList(head)
{
    var curr = head;
    a = curr;
    b = curr.next;
    var aRef = a, bRef = b;
    moveNode(aRef, bRef);
}
 
function display(head)
{
    var curr = head;
    while (curr != null)
    {
        document.write(curr.data+ " ");
        curr = curr.next;
    }
}
 
// Driver code
var head = null;
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
 
alternateSplitLinkedList(head);
document.write("a : ");
display(a);
document.write("<br>b : ");
display(b);
 
</script>
 
 
Output: 
a : 1 3 5 7  b : 2 4 6 

 

Time Complexity: O(N)
Auxiliary Space: O(N)



Next Article
Reverse alternate K nodes in a Singly Linked List

A

aditya1011
Improve
Article Tags :
  • DSA
  • Linked List
  • Misc
  • Recursion
Practice Tags :
  • Linked List
  • Misc
  • Recursion

Similar Reads

  • Alternating split of a given Singly Linked List | Set 1
    Write a function AlternatingSplit() that takes one list and divides up its nodes to make two smaller lists 'a' and 'b'. The sublists should be made from alternating elements in the original list. So if the original list is 0->1->0->1->0->1 then one sublist should be 0->0->0 and
    15+ min read
  • Modify contents of Linked List - Recursive approach
    Given a singly linked list containing n nodes. Modify the value of first half nodes such that 1st node’s new value is equal to the last node’s value minus first node’s current value, 2nd node’s new value is equal to the second last node’s value minus 2nd node’s current value, likewise for first half
    11 min read
  • Reverse alternate K nodes in a Singly Linked List
    Given a linked list, The task is to reverse alternate k nodes. If the number of nodes left at the end of the list is fewer than k, reverse these remaining nodes or leave them in their original order, depending on the alternation pattern. Example: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -
    15+ min read
  • Reverse a singly Linked List in groups of given size (Recursive Approach)
    Given a Singly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should be considered as a group and must be reversed. Example: Input: head: 1 -> 2 -> 3 -> 4 -> 5 ->
    9 min read
  • Javascript Program For Alternating Split Of A Given Singly Linked List- Set 1
    Write a function AlternatingSplit() that takes one list and divides up its nodes to make two smaller lists 'a' and 'b'. The sublists should be made from alternating elements in the original list. So if the original list is 0->1->0->1->0->1 then one sublist should be 0->0->0 and
    3 min read
  • Recursive function to delete k-th node from linked list
    Given a singly linked list, delete a node at the kth position without using the loop. Examples: Input : list = 9->8->3->5->2->1 k = 4 Output : 9->8->3->2->1 Input : list = 0->0->1->6->2->3 k = 3 Output : 0->0->6->2->3 We recursively reduce the va
    6 min read
  • Given a linked list, reverse alternate nodes and append at the end
    Given a linked list, reverse alternate nodes and append them to the end of the list. Extra allowed space is O(1) Examples: Input: 1->2->3->4->5->6 Output: 1->3->5->6->4->2 Explanation: Two lists are 1->3->5 and 2->4->6, reverse the 2nd list: 6->4->2. M
    11 min read
  • Reverse a sublist of linked list
    Given a linked list and positions m and n. We need to reverse the linked list from position m to n. Examples: Input : linkedlist : 10->20->30->40->50->60->70->NULL , m = 3 and n = 6Output : 10->20->60->50->40->30->70->NULLExplanation: Linkedlist reversed sta
    15+ min read
  • Reverse alternate K nodes in a Singly Linked List - Iterative Solution
    Given a linked list and an integer K, the task is to reverse every alternate K nodes.Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL, K = 3 Output: 3 2 1 4 5 6 9 8 7Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL, K =
    12 min read
  • Reverse a Linked List in groups of given size (Iterative Approach)
    Given a Singly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should be considered as a group and must be reversed. Examples: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences