मुख्य कंटेंट तक स्किप करें

Segregate even and odd nodes in a linked list

KANISHKA GUPTA
EditReport

Segregate Even and Odd Nodes in a Linked List

Problem Description

In a linked list, the goal is to rearrange the nodes such that all even-valued nodes appear before all odd-valued nodes. The relative order of the even and odd nodes should remain the same as in the original list. For example, given the linked list 1 -> 2 -> 3 -> 4 -> 5, the output should be 2 -> 4 -> 1 -> 3 -> 5.

Video Explanation

Approach

To segregate even and odd nodes, we can use the following approach:

  1. Initialize Pointers:

    • Use two pointers to keep track of the even and odd nodes separately: evenHead and oddHead.
    • Create two additional pointers to build the segregated list: evenTail and oddTail.
  2. Traverse the List:

    • Iterate through the linked list and for each node:
      • If the node's value is even, append it to the even list.
      • If the node's value is odd, append it to the odd list.
  3. Combine the Lists:

    • After traversing the list, connect the end of the even list to the head of the odd list.
  4. Handle Edge Cases:

    • If the even list is empty, return the head of the odd list.
    • If the odd list is empty, return the head of the even list.

Implementation

Solutions

#include <iostream>

class Node {
public:
int value;
Node* next;

Node(int val) : value(val), next(nullptr) {}
};

class LinkedList {
public:
Node* head;

LinkedList() : head(nullptr) {}

// Function to segregate even and odd nodes
Node* segregateEvenOdd() {
if (!head) return nullptr;

Node* evenHead = nullptr;
Node* oddHead = nullptr;
Node* evenTail = nullptr;
Node* oddTail = nullptr;

Node* current = head;

while (current) {
if (current->value % 2 == 0) {
// Append to even list
if (!evenHead) {
evenHead = current;
evenTail = evenHead;
} else {
evenTail->next = current;
evenTail = evenTail->next;
}
} else {
// Append to odd list
if (!oddHead) {
oddHead = current;
oddTail = oddHead;
} else {
oddTail->next = current;
oddTail = oddTail->next;
}
}
current = current->next;
}

// Combine even and odd lists
if (evenTail) {
evenTail->next = oddHead;
}

if (oddTail) {
oddTail->next = nullptr; // End the odd list
}

return evenHead ? evenHead : oddHead; // Return the head of the combined list
}

// Function to add a new node at the end of the list
void append(int value) {
Node* newNode = new Node(value);
if (!head) {
head = newNode;
return;
}

Node* current = head;
while (current->next) {
current = current->next;
}
current->next = newNode;
}

// Function to print the linked list
void printList() {
Node* current = head;
while (current) {
std::cout << current->value << " -> ";
current = current->next;
}
std::cout << "nullptr" << std::endl;
}
};

// Example usage
int main() {
LinkedList ll;
ll.append(1);
ll.append(2);
ll.append(3);
ll.append(4);
ll.append(5);

std::cout << "Original list: ";
ll.printList();

ll.head = ll.segregateEvenOdd();

std::cout << "Segregated list: ";
ll.printList();

return 0;
}
Track Your Progress

Done with this topic? Mark it as complete to track your progress.

💬 Discuss this page

Have a question or spot something confusing in "Segregate even and odd nodes in a linked list"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.