Easy C# version to understand O(n) time and O(1) space used


  • -2
    P

    class Solution
    {
    public ListNode ModifyTheOddEvenList(ListNode head){
    if (head == null || head.next == null){
    return head;
    }
    ListNode current = head;
    ListNode front = head.next;
    ListNode laterTemp = head.next; // to bind the two lists in the end
    while (current.next != null && front.next != null)
    {
    current.next = front.next;
    front.next = front.next.next;
    current = current.next;
    front = front.next;
    }
    current.next = laterTemp;
    return head;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.