C# Solution


  • 0
        public ListNode OddEvenList(ListNode head) {
            if (head == null || head.next == null || head.next.next == null)
                return head;
            
            ListNode oddNode = head, evenNode = head.next, tempNode1 = evenNode.next, tempNode2 = tempNode1.next;
            int index = 3;
            
            evenNode.next = null;
            
            while (tempNode1 != null)
            {
                if (index % 2 != 0)
                {
                    tempNode1.next = oddNode.next;
                    oddNode.next = tempNode1;
                    oddNode = oddNode.next;
                }
                else
                {
                    evenNode.next = tempNode1;
                    evenNode = evenNode.next;
                    evenNode.next = null;
                }
                
                tempNode1 = tempNode2;
                if (tempNode1 != null)
                    tempNode2 = tempNode1.next;
                
                index++;
            }
            
            return head;
        }

Log in to reply
 

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