
LeetCode 328: Odd Even Linked List β Clean and Easy Explanation with Multiple Approaches
Try the ProblemYou can practice the problem here:https://leetcode.com/problems/odd-even-linked-list/Problem Description (In Very Simple Words)You are given the head of a linked list.Your task is:π Rearrange the list such that:All nodes at odd positions come firstFollowed by all nodes at even positionsImportant Clarificationβ This problem is about positions (indices), NOT values.1st node β Odd2nd node β Even3rd node β Odd4th node β EvenExample WalkthroughExample 1Input:[1,2,3,4,5]Positions:1(odd), 2(even), 3(odd), 4(even), 5(odd)Output:[1,3,5,2,4]Example 2Input:[2,1,3,5,6,4,7]Output:[2,3,6,7,1,5,4]Constraints0 <= number of nodes <= 10^4-10^6 <= Node.val <= 10^6Core Idea of the ProblemWe need to:Separate nodes into two groups:Odd index nodesEven index nodesMaintain their original orderFinally:π Attach even list after odd listThinking About Different ApproachesWhen solving this problem, multiple approaches may come to mind:Approach 1: Create New ListsIdeaTraverse the listCreate new nodes for odd and even positionsBuild two separate listsMerge themProblem with This Approachβ Uses extra spaceβ Not optimal (violates O(1) space requirement)β Code becomes messy and harder to maintainYour approach is logically correct, but:π It is not optimal and can be simplified a lot.Approach 2: Brute Force Using Array/ListIdeaStore nodes in an arrayRearrange based on indicesRebuild linked listComplexityTime: O(n)Space: O(n) β (Not allowed)Approach 3: Optimal In-Place Approach (Best Solution)This is the cleanest and most important approach.Optimal Approach: Two Pointer Technique (In-Place)IdeaInstead of creating new nodes:π We rearrange the existing nodesWe maintain:odd β points to odd nodeseven β points to even nodesevenHead β stores start of even listVisualizationInput:1 β 2 β 3 β 4 β 5We separate into:Odd: 1 β 3 β 5Even: 2 β 4Final:1 β 3 β 5 β 2 β 4Step-by-Step LogicStep 1: Initialize pointersodd = headeven = head.nextevenHead = head.nextStep 2: Traverse the listWhile:even != null && even.next != nullUpdate:odd.next = odd.next.nexteven.next = even.next.nextMove pointers forward:odd = odd.nexteven = even.nextStep 3: Mergeodd.next = evenHeadClean Java Implementation (Optimal)class Solution { public ListNode oddEvenList(ListNode head) { // Edge case if(head == null || head.next == null) return head; // Initialize pointers ListNode odd = head; ListNode even = head.next; ListNode evenHead = even; // Rearranging nodes while(even != null && even.next != null){ odd.next = odd.next.next; // Link next odd even.next = even.next.next; // Link next even odd = odd.next; even = even.next; } // Attach even list after odd list odd.next = evenHead; return head; }}Dry Run (Important)Input:1 β 2 β 3 β 4 β 5Steps:Iteration 1:odd β 1, even β 2Link:1 β 32 β 4Iteration 2:odd β 3, even β 4Link:3 β 54 β nullFinal connection:5 β 2Result:1 β 3 β 5 β 2 β 4Time ComplexityO(n)We traverse the list once.Space ComplexityO(1)No extra space used.Why This Approach is BestFeatureResultExtra Spaceβ NoneClean Codeβ YesEfficientβ O(n)Interview Friendlyβ HighlyCommon Mistakesβ Confusing values with positionsβ Creating new nodes unnecessarilyβ Forgetting to connect even list at the endβ Breaking the list accidentallyKey LearningThis problem teaches:In-place linked list manipulationPointer handlingList partitioningFinal ThoughtsThe Odd Even Linked List problem is a classic example of how powerful pointer manipulation can be.Even though creating new nodes might seem easier at first, the in-place approach is:π Fasterπ Cleanerπ Interview optimizedπ Tip: Whenever you are asked to rearrange a linked list, always think:"Can I solve this by just changing pointers instead of creating new nodes?"Thatβs the key to mastering linked list problems π










