C# solution: two pointers


  • 0
    B
    public class Solution
    {
    	public ListNode OddEvenList(ListNode head)
    	{
    		if (head == null) return null;
    
    		var slow = head;
    		var fast = head.next;
    		
    		if (fast == null) return head;
    
    		var oddHead = slow;
    		var evenHead = fast;
    
    		var curOdd = oddHead;
    		var curEven = evenHead;
    
    		while (fast != null && fast.next != null)
    		{
    			slow = slow.next.next;
    			fast = fast.next.next;
    
    			curOdd.next = slow;
    			curEven.next = fast;
    
    			curOdd = curOdd.next;
    			curEven = curEven.next;
    		}
    
    
    		curOdd.next = evenHead;
    
    		return oddHead;
    	}
    }
    

Log in to reply
 

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