Segregate Odd and Even Nodes

Medium Problems of Singly Linked List DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Segregate even and odd nodes in LinkedList, Linked List, Node, Pointer, Algorithm, Time Complexity, Space Complexity, Big O Notation, Loops, Conditional Statements, array partitioning, node operations, linked list, time complexity analysis, two pointer technique, Partitioning Strategies.

Problem Statement Given the head of a singly linked list, modify the list such that all even valued nodes appear before all odd valued nodes. The relative order of even valued nodes and odd valued nodes must be preserved. Input The head of a singly linked list. Output The head of the modified linked list. Examples Example 1: Input: 1→2→3→4→5→6→Null Output: 2→4→6→1→3→5→Null Explanation: Odd nodes are 1, 3, 5. Even nodes are 2, 4, 6. In the modified linked list, all even nodes come before all odd nodes, and their respective original orders are maintained. Example 2: Input: 1→3→5→Null Output: 1→3→5→Null Explanation: As there are no even nodes in the linked list, the modified linked list is the same as the original linked list. Example 3: Input: 2→4→6→8→Null Output: 2→4→6→8→Null Explanation: As there are no odd nodes in the linked list, the modified linked list is the same as the original linked list.