Odd-Even Linked List solution #leetcode328

Intuition: Make two dummy nodes OddHead and EvenHead, all the odd indices goes to OddHead, and even indices go to EvenHead.

Approach:
OddHead will have all the odd ones, EvenHead will have all the even ones
connect them via a pointer.

Java Solution:-…


This content originally appeared on DEV Community and was authored by Reaper

Intuition: Make two dummy nodes OddHead and EvenHead, all the odd indices goes to OddHead, and even indices go to EvenHead.

Approach:
OddHead will have all the odd ones, EvenHead will have all the even ones
connect them via a pointer.

Java Solution:-

public ListNode oddEvenList(ListNode head){
    if(head == null || head.next == null) return head;
    ListNode oddHead = new ListNode(-1);
    ListNode evenHead = new ListNode(-1);
    ListNode oddptr = oddHead;
    ListNode evenptr = evenHead;
    ListNode current = head;
    int index = 1;
    while(current!= null){
       if(index%2 == 1){
         oddptr.next = current;
         oddptr = oddptr.next;
        }
        else{
          evenptr.next = current;
          evenptr = evenptr.next;
        }
        current = current.next;
        index++;
     }
     evenptr.next = null;
     oddptr.next = evenHead.next;
     return oddHead.next;
}


This content originally appeared on DEV Community and was authored by Reaper


Print Share Comment Cite Upload Translate Updates
APA

Reaper | Sciencx (2025-07-03T15:19:22+00:00) Odd-Even Linked List solution #leetcode328. Retrieved from https://www.scien.cx/2025/07/03/odd-even-linked-list-solution-leetcode328/

MLA
" » Odd-Even Linked List solution #leetcode328." Reaper | Sciencx - Thursday July 3, 2025, https://www.scien.cx/2025/07/03/odd-even-linked-list-solution-leetcode328/
HARVARD
Reaper | Sciencx Thursday July 3, 2025 » Odd-Even Linked List solution #leetcode328., viewed ,<https://www.scien.cx/2025/07/03/odd-even-linked-list-solution-leetcode328/>
VANCOUVER
Reaper | Sciencx - » Odd-Even Linked List solution #leetcode328. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/03/odd-even-linked-list-solution-leetcode328/
CHICAGO
" » Odd-Even Linked List solution #leetcode328." Reaper | Sciencx - Accessed . https://www.scien.cx/2025/07/03/odd-even-linked-list-solution-leetcode328/
IEEE
" » Odd-Even Linked List solution #leetcode328." Reaper | Sciencx [Online]. Available: https://www.scien.cx/2025/07/03/odd-even-linked-list-solution-leetcode328/. [Accessed: ]
rf:citation
» Odd-Even Linked List solution #leetcode328 | Reaper | Sciencx | https://www.scien.cx/2025/07/03/odd-even-linked-list-solution-leetcode328/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.