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 Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
Alternate Odd and Even Nodes in a Singly Linked List
Next article icon

Alternating split of a given Singly Linked List | Set 1

Last Updated : 14 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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 the other should be 1->1->1. 
 

Recommended Problem
Split Linked List Alternatingly
Solve Problem

Method 1(Simple) 
The simplest approach iterates over the source list and pull nodes off the source and alternately put them at the front (or beginning) of ‘a’ and b’. The only strange part is that the nodes will be in the reverse order that occurred in the source list. Method 2 inserts the node at the end by keeping track of the last node in sublists. 
 

C++




/* C++ Program to alternatively split
a linked list into two halves */
#include <bits/stdc++.h>
using namespace std;
 
/* Link list node */
class Node
{
    public:
    int data;
    Node* next;
};
 
/* pull off the front node of
the source and put it in dest */
void MoveNode(Node** destRef, Node** sourceRef) ;
 
/* Given the source list, split its
nodes into two shorter lists. If we number
the elements 0, 1, 2, ... then all the even
elements should go in the first list, and
all the odd elements in the second. The
elements in the new lists may be in any order. */
void AlternatingSplit(Node* source, Node** aRef,
                            Node** bRef)
{
    /* split the nodes of source
    to these 'a' and 'b' lists */
    Node* a = NULL;
    Node* b = NULL;
         
    Node* current = source;
    while (current != NULL)
    {
        MoveNode(&a, &current); /* Move a node to list 'a' */
        if (current != NULL)
        {
            MoveNode(&b, &current); /* Move a node to list 'b' */
        }
    }
    *aRef = a;
    *bRef = b;
}
 
/* Take the node from the front of
the source, and move it to the front
of the dest. It is an error to call
this with the source list empty.
     
Before calling MoveNode():
source == {1, 2, 3}
dest == {1, 2, 3}
         
After calling MoveNode():
source == {2, 3}    
dest == {1, 1, 2, 3}    
*/
void MoveNode(Node** destRef, Node** sourceRef)
{
    /* the front source node */
    Node* newNode = *sourceRef;
    assert(newNode != NULL);
         
    /* Advance the source pointer */
    *sourceRef = newNode->next;
         
    /* Link the old dest of the new node */
    newNode->next = *destRef;
         
    /* Move dest to point to the new node */
    *destRef = newNode;
}
 
/* UTILITY FUNCTIONS */
/* Function to insert a node at
the beginning of the linked list */
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = new Node();
     
    /* put in the data */
    new_node->data = new_data;
     
    /* link the old list of the new node */
    new_node->next = (*head_ref);    
     
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
/* Function to print nodes
in a given linked list */
void printList(Node *node)
{
    while(node!=NULL)
    {
    cout<<node->data<<" ";
    node = node->next;
    }
}
 
/* Driver code*/
int main()
{
    /* Start with the empty list */
    Node* head = NULL;
    Node* a = NULL;
    Node* b = NULL;
     
    /* Let us create a sorted linked list to test the functions
    Created linked list will be 0->1->2->3->4->5 */
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);                                
    push(&head, 0);
     
    cout<<"Original linked List: ";
    printList(head);
     
    /* Remove duplicates from linked list */
    AlternatingSplit(head, &a, &b);
     
    cout<<"\nResultant Linked List 'a' : ";
    printList(a);        
     
    cout<<"\nResultant Linked List 'b' : ";
    printList(b);        
     
    return 0;
}
 
// This code is contributed by rathbhupendra
 
 

C




/*Program to alternatively split a linked list into two halves */
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
 
/* Link list node */
struct Node
{
    int data;
    struct Node* next;
};
 
/* pull off the front node of the source and put it in dest */
void MoveNode(struct Node** destRef, struct Node** sourceRef) ;
 
/* Given the source list, split its nodes into two shorter lists.
  If we number the elements 0, 1, 2, ... then all the even elements
  should go in the first list, and all the odd elements in the second.
  The elements in the new lists may be in any order. */
void AlternatingSplit(struct Node* source, struct Node** aRef,
                            struct Node** bRef)
{
  /* split the nodes of source to these 'a' and 'b' lists */
  struct Node* a = NULL;
  struct Node* b = NULL;
   
  struct Node* current = source;
  while (current != NULL)
  {
    MoveNode(&a, &current); /* Move a node to list 'a' */
    if (current != NULL)
    {
       MoveNode(&b, &current); /* Move a node to list 'b' */
    }
  }
  *aRef = a;
  *bRef = b;
}
 
/* Take the node from the front of the source, and move it to the front of the dest.
   It is an error to call this with the source list empty.
    
   Before calling MoveNode():
   source == {1, 2, 3}  
   dest == {1, 2, 3}
       
   After calling MoveNode():
   source == {2, 3}     
   dest == {1, 1, 2, 3}     
*/
void MoveNode(struct Node** destRef, struct Node** sourceRef)
{
  /* the front source node  */
  struct Node* newNode = *sourceRef;
  assert(newNode != NULL);
   
  /* Advance the source pointer */
  *sourceRef = newNode->next;
   
  /* Link the old dest of the new node */
  newNode->next = *destRef;
   
  /* Move dest to point to the new node */
  *destRef = newNode;
}
 
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning of the linked list */
void push(struct node** head_ref, int new_data)
{
  /* allocate node */
  struct Node* new_node =
            (struct Node*) malloc(sizeof(struct Node));
 
  /* put in the data  */
  new_node->data  = new_data;
 
  /* link the old list of the new node */
  new_node->next = (*head_ref);    
 
  /* move the head to point to the new node */
  (*head_ref)    = new_node;
}
 
/* Function to print nodes in a given linked list */
void printList(struct Node *node)
{
  while(node!=NULL)
  {
   printf("%d ", node->data);
   node = node->next;
  }
}
 
/* Driver program to test above functions*/
int main()
{
  /* Start with the empty list */
  struct Node* head = NULL;
  struct Node* a = NULL;
  struct Node* b = NULL; 
 
  /* Let us create a sorted linked list to test the functions
   Created linked list will be 0->1->2->3->4->5 */
  push(&head, 5);
  push(&head, 4);
  push(&head, 3);
  push(&head, 2);
  push(&head, 1);                                   
  push(&head, 0); 
 
  printf("\n Original linked List:  ");
  printList(head);
 
  /* Remove duplicates from linked list */
  AlternatingSplit(head, &a, &b);
 
  printf("\n Resultant Linked List 'a' ");
  printList(a);           
 
  printf("\n Resultant Linked List 'b' ");
  printList(b);           
 
  getchar();
  return 0;
}
 
 

Java




import java.util.*;
 
// Linked list node
class Node {
  int data;
  Node next;
 
  Node(int data) {
    this.data = data;
    next = null;
  }
}
 
class Main
{
   
  // Given the source list, split its nodes into two shorter lists.
  // All the even elements should go in the first list, and all the odd
  // elements in the second. The elements in the new lists may be in any order.
  static void AlternatingSplit(Node source, Node[] aRef, Node[] bRef) {
    Node a = null, b = null;
    Node current = source;
    int count = 0;
 
    while (current != null) {
      if (count % 2 == 0) {
        // Move a node to list 'a'
        if (a == null) {
          aRef[0] = current;
          a = current;
        } else {
          a.next = current;
          a = a.next;
        }
      } else {
        // Move a node to list 'b'
        if (b == null) {
          bRef[0] = current;
          b = current;
        } else {
          b.next = current;
          b = b.next;
        }
      }
      current = current.next;
      count++;
    }
 
    if (a != null) {
      a.next = null;
    }
 
    if (b != null) {
      b.next = null;
    }
  }
 
  // Function to print nodes in a given linked list
  static void printList(Node node) {
    while (node != null) {
      System.out.print(node.data + " ");
      node = node.next;
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
     
    // Start with the empty list
    Node head = null;
 
    // Let us create a sorted linked list to test the functions
    // Created linked list will be 0->1->2->3->4->5
    for (int i = 5; i >= 0; i--) {
      Node newNode = new Node(i);
      newNode.next = head;
      head = newNode;
    }
 
    System.out.print("Original linked List: ");
    printList(head);
 
    Node[] aRef = new Node[1];
    Node[] bRef = new Node[1];
 
    // Remove duplicates from linked list
    AlternatingSplit(head, aRef, bRef);
 
    System.out.print("\nResultant Linked List 'a': ");
    printList(aRef[0]);
 
    System.out.print("\nResultant Linked List 'b': ");
    printList(bRef[0]);
  }
}
 
 

Python




# Python program to alternatively split
# a linked list into two halves
 
# Node class
class Node:
     
    def __init__(self, data, next = None):
         
        self.data = data
        self.next = None
 
class LinkedList:
     
    def __init__(self):
         
        self.head = None
     
    # Given the source list, split its
    # nodes into two shorter lists. If we number
    # the elements 0, 1, 2, ... then all the even
    # elements should go in the first list, and
    # all the odd elements in the second. The
    # elements in the new lists may be in any order.
    def AlternatingSplit(self, a, b):
         
        first = self.head
        second = first.next
         
        while (first is not None and
              second is not None and
          first.next is not None):
               
              # Move a node to list 'a'
              self.MoveNode(a, first)
               
              # Move a node to list 'b'
              self.MoveNode(b, second)
               
              first = first.next.next
               
              if first is None:
                break
               
              second = first.next
             
    # Pull off the front node of the
    # source and put it in dest
    def MoveNode(self, dest, node):
         
        # Make the new node
        new_node = Node(node.data)
         
        if dest.head is None:
            dest.head = new_node
        else:
             
            # Link the old dest of the new node
            new_node.next = dest.head
             
            # Move dest to point to the new node
            dest.head = new_node
 
    # UTILITY FUNCTIONS
    # Function to insert a node at 
    # the beginning of the linked list
    def push(self, data):
         
        # 1 & 2 allocate the Node &
        # put the data
         
        new_node = Node(data)
         
        # Make the next of new Node as head
        new_node.next = self.head
         
        # Move the head to point to new Node
        self.head = new_node
         
    # Function to print nodes
    # in a given linked list
    def printList(self):
         
        temp = self.head
        while temp:
            print temp.data,
            temp = temp.next
             
        print("")
 
# Driver Code
if __name__ == "__main__":
     
    # Start with empty list
    llist = LinkedList()
    a = LinkedList()
    b = LinkedList()
     
    # Created linked list will be
    # 0->1->2->3->4->5
    llist.push(5)
    llist.push(4)
    llist.push(3)
    llist.push(2)
    llist.push(1)
    llist.push(0)
     
    llist.AlternatingSplit(a, b)
     
    print "Original Linked List: ",
    llist.printList()
     
    print "Resultant Linked List 'a' : ",
    a.printList()
     
    print "Resultant Linked List 'b' : ",
    b.printList()
     
# This code is contributed by kevalshah5
 
 

C#




// C# program to alternatively split
// a linked list into two halves
using System;
using System.Collections.Generic;
 
public class Node{
  public int data;
  public Node next;
 
  public Node(int item){
    data = item;
    next = null;
  }
}
 
public class LinkedList{
  Node head;
  // Given the source list, split its
  // nodes into two shorter lists. If we number
  // the elements 0, 1, 2, ... then all the even
  // elements should go in the first list, and
  // all the odd elements in the second. The
  // elements in the new lists may be in any order.
  public void AlternatingSplit(LinkedList a, LinkedList b){
    Node first = head;
    Node second = first.next;
 
    while(first != null && second != null && first.next != null)
    {
       
      // move a node to list 'a'
      MoveNode(a, first);
 
      // move a node to list 'b'
      MoveNode(b, second);
 
      first = first.next.next;
      if(first == null)
        break;
 
      second = first.next;
    }
  }
 
  // Pull off the front node of the
  // source and put it in dest
  public void MoveNode(LinkedList dest, Node node){
 
    // Make the new node
    Node new_node = new Node(node.data);
 
    if(dest.head == null)
      dest.head = new_node;
    else{
      // Link the old dest of the new node
      new_node.next = dest.head;
 
      // Move dest to point to the new node
      dest.head = new_node;
    }
  }
 
  // UTILITY FUNCTIONS
  // Function to insert a node at 
  // the beginning of the linked list
  void push(int data){
    // 1 & 2 allocate the Node &
    // put the data
    Node new_node = new Node(data);
 
    // Make the next of new Node as head
    new_node.next = head;
 
    // Move the head to point to new Node
    head = new_node;
  }
 
  // Function to print nodes
  // in a given linked list
  public void printList(){
    Node temp = head;
    while(temp != null){
      Console.Write(temp.data + " ");
      temp = temp.next;
    }
    Console.WriteLine("");
  }
  public static void Main(string[] args){
    LinkedList llist = new LinkedList();
    LinkedList a = new LinkedList();
    LinkedList b = new LinkedList();
 
    // created linked list will be
    // 0->1->2->3->4->5
    llist.push(5);
    llist.push(4);
    llist.push(3);
    llist.push(2);
    llist.push(1);
    llist.push(0);
 
    llist.AlternatingSplit(a, b);
 
    Console.WriteLine("Original Linked List : ");
    llist.printList();
 
    Console.WriteLine("Resultant Linked List 'a' : ");
    a.printList();
 
    Console.WriteLine("Resultant Linked List 'b' : ");
    b.printList();
  }
}
 
// THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGAWRAL2852002)
 
 

Javascript




<script>
// JavaScript program to alternatively split
// a linked list into two halves
 
// Node class
class Node{
     
    constructor(data,next = null){
        this.data = data
        this.next = next
    }
}
 
class LinkedList
{   
    constructor()
    {   
        this.head = null
    }
     
    // Given the source list, split its
    // nodes into two shorter lists. If we number
    // the elements 0, 1, 2, ... then all the even
    // elements should go in the first list, and
    // all the odd elements in the second. The
    // elements in the new lists may be in any order.
    AlternatingSplit(a, b){
         
        let first = this.head
        let second = first.next
         
        while (first != null &&
              second != null &&
          first.next != null){
               
              // Move a node to list 'a'
              this.MoveNode(a, first)
               
              // Move a node to list 'b'
              this.MoveNode(b, second)
               
              first = first.next.next
               
              if(first == null)
                break
               
              second = first.next
        }
    }
             
    // Pull off the front node of the
    // source and put it in dest
    MoveNode(dest, node){
         
        // Make the new node
        let new_node = new Node(node.data)
         
        if(dest.head == null)
            dest.head = new_node
        else{
             
            // Link the old dest of the new node
            new_node.next = dest.head
             
            // Move dest to point to the new node
            dest.head = new_node
        }
    }
 
    // UTILITY FUNCTIONS
    // Function to insert a node at 
    // the beginning of the linked list
    push(data){
         
        // 1 & 2 allocate the Node &
        // put the data
         
        let new_node = new Node(data)
         
        // Make the next of new Node as head
        new_node.next = this.head
         
        // Move the head to point to new Node
        this.head = new_node
    }
         
    // Function to print nodes
    // in a given linked list
    printList(){
         
        let temp = this.head
        while(temp){
            document.write(temp.data," ");
            temp = temp.next
        }
             
        document.write("</br>")
    }
}
 
// Driver Code
     
// Start with empty list
let llist = new LinkedList()
let a = new LinkedList()
let b = new LinkedList()
 
// Created linked list will be
// 0->1->2->3->4->5
llist.push(5)
llist.push(4)
llist.push(3)
llist.push(2)
llist.push(1)
llist.push(0)
 
llist.AlternatingSplit(a, b)
 
document.write("Original Linked List: ");
llist.printList()
 
document.write("Resultant Linked List 'a' : ");
a.printList()
 
document.write("Resultant Linked List 'b' : ");
b.printList()
     
// This code is contributed by shinjanpatra
 
</script>
 
 

Output: 
 

Original linked List: 0 1 2 3 4 5  Resultant Linked List 'a' : 4 2 0  Resultant Linked List 'b' : 5 3 1

Time Complexity: O(n) 

where n is a number of nodes in the given linked list.

Auxiliary Space: O(1)

As constant extra space is used.
Method 2(Using Dummy Nodes) 
Here is an alternative approach that builds the sub-lists in the same order as the source list. The code uses temporary dummy header nodes for the ‘a’ and ‘b’ lists as they are being built. Each sublist has a “tail” pointer that points to its current last node — that way new nodes can be appended to the end of each list easily. The dummy nodes give the tail pointers something to point to initially. The dummy nodes are efficient in this case because they are temporary and allocated in the stack. Alternately, local “reference pointers” (which always point to the last pointer in the list instead of to the last node) could be used to avoid Dummy nodes. 
 

C++




void AlternatingSplit(Node* source,
                      Node** aRef, Node** bRef)
{
    Node aDummy;
     
    /* points to the last node in 'a' */
    Node* aTail = &aDummy;
    Node bDummy;
     
    /* points to the last node in 'b' */
    Node* bTail = &bDummy;
    Node* current = source;
    aDummy.next = NULL;
    bDummy.next = NULL;
    while (current != NULL)
    {
        MoveNode(&(aTail->next), ¤t); /* add at 'a' tail */
        aTail = aTail->next; /* advance the 'a' tail */
        if (current != NULL)
        {
            MoveNode(&(bTail->next), ¤t);
            bTail = bTail->next;
        }
    }
    *aRef = aDummy.next;
    *bRef = bDummy.next;
}
 
// This code is contributed
// by rathbhupendra
 
 

C




void AlternatingSplit(struct Node* source, struct Node** aRef,
                            struct Node** bRef)
{
  struct Node aDummy;
  struct Node* aTail = &aDummy; /* points to the last node in 'a' */
  struct Node bDummy;
  struct Node* bTail = &bDummy; /* points to the last node in 'b' */
  struct Node* current = source;
  aDummy.next = NULL;
  bDummy.next = NULL;
  while (current != NULL)
  {
    MoveNode(&(aTail->next), &current); /* add at 'a' tail */
    aTail = aTail->next; /* advance the 'a' tail */
    if (current != NULL)
    {
      MoveNode(&(bTail->next), &current);
      bTail = bTail->next;
    }
  }
  *aRef = aDummy.next;
  *bRef = bDummy.next;
}
 
 

Java




static void AlternatingSplit(Node source, Node aRef,
                            Node bRef)
{
  Node aDummy = new Node();
  Node aTail = aDummy; /* points to the last node in 'a' */
  Node bDummy = new Node();
  Node bTail = bDummy; /* points to the last node in 'b' */
  Node current = source;
  aDummy.next = null;
  bDummy.next = null;
  while (current != null)
  {
    MoveNode((aTail.next), current); /* add at 'a' tail */
    aTail = aTail.next; /* advance the 'a' tail */
    if (current != null)
    {
      MoveNode((bTail.next), current);
      bTail = bTail.next;
    }
  }
  aRef = aDummy.next;
  bRef = bDummy.next;
}
 
// This code is contributed by rutvik_56
 
 

Python3




def AlternatingSplit(source, aRef,  bRef):
   
  aDummy = Node();
  aTail = aDummy; ''' points to the last Node in 'a' '''
  bDummy = Node();
  bTail = bDummy; ''' points to the last Node in 'b' '''
  current = source;
  aDummy.next = None;
  bDummy.next = None;
  while (current != None):
    MoveNode((aTail.next), current); ''' add at 'a' tail '''
    aTail = aTail.next; ''' advance the 'a' tail '''
    if (current != None):
      MoveNode((bTail.next), current);
      bTail = bTail.next;
     
  aRef = aDummy.next;
  bRef = bDummy.next;
 
# This code is contributed by umadevi9616
 
 

C#




static void AlternatingSplit(Node source, Node aRef,
                            Node bRef)
{
  Node aDummy = new Node();
  Node aTail = aDummy; /* points to the last node in 'a' */
  Node bDummy = new Node();
  Node bTail = bDummy; /* points to the last node in 'b' */
  Node current = source;
  aDummy.next = null;
  bDummy.next = null;
  while (current != null)
  {
    MoveNode((aTail.next), current); /* add at 'a' tail */
    aTail = aTail.next; /* advance the 'a' tail */
    if (current != null)
    {
      MoveNode((bTail.next), current);
      bTail = bTail.next;
    }
  }
  aRef = aDummy.next;
  bRef = bDummy.next;
}
 
// This code is contributed by pratham_76
 
 

Javascript




<script>
function AlternatingSplit( source,  aRef,
                             bRef)
{
  var aDummy = new Node();
  var aTail = aDummy; /* points to the last node in 'a' */
  var bDummy = new Node();
  var bTail = bDummy; /* points to the last node in 'b' */
  var current = source;
  aDummy.next = null;
  bDummy.next = null;
  while (current != null)
  {
    MoveNode((aTail.next), current); /* add at 'a' tail */
    aTail = aTail.next; /* advance the 'a' tail */
    if (current != null)
    {
      MoveNode((bTail.next), current);
      bTail = bTail.next;
    }
  }
  aRef = aDummy.next;
  bRef = bDummy.next;
}
 
// This code contributed by aashish1995
</script>
 
 

Time Complexity: O(n) 

where n is number of node in the given linked list.

Auxiliary Space: O(1)

As Constant extra space is used.

Source: http://cslibrary.stanford.edu/105/LinkedListProblems.pdf
Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem.
 



Next Article
Alternate Odd and Even Nodes in a Singly Linked List
author
kartik
Improve
Article Tags :
  • DSA
  • Linked List
Practice Tags :
  • Linked List

Similar Reads

  • 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
  • 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 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
  • 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
  • Recursive approach for alternating split of Linked List
    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'
    7 min read
  • Delete a Linked List node at a given position
    Given a singly linked list and a position (1-based indexing), the task is to delete a linked list node at the given position. Note: Position will be valid (i.e, 1 <= position <= linked list length) Example: Input: position = 2, Linked List = 8->2->3->1->7Output: Linked List = 8-
    8 min read
  • Print alternate nodes of a linked list using recursion
    Given a linked list, print alternate nodes of this linked list. Examples : Input : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 Output : 1 -> 3 -> 5 -> 7 -> 9 Input : 10 -> 9 Output : 10 Recursive Approach : Initialize a static variable(say flag) If flag
    6 min read
  • Find the Previous Closest Smaller Node in a Singly Linked List
    Given a singly linked list, the task is to find the previous closest smaller node for every node in a linked list. Examples: Input: 5 -> 6 -> 8 -> 2 -> 3 -> 4Output: -1 -> 5 -> 6 -> -1 -> 2 -> 3Explanation: For the first node 5, there is no previous closest smaller node
    13 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
  • 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
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