A barely work java solution


  • 0
    S
    public class Solution {
        public ListNode oddEvenList(ListNode head) {
            ListNode [] twoLists = new ListNode[2];
            twoLists[0] = new ListNode(0);
            twoLists[1] = new ListNode(0);
    
            ListNode p0 = twoLists[0];
            ListNode p1 = twoLists[1];
    
            int index = 0;
            ListNode p = head;
            while (p != null) {
                twoLists[index].next = p;
                twoLists[index] = p;
                p = p.next;
                index = 1 - index;
            }
    
            twoLists[0].next = p1.next;
            twoLists[1].next = null;
            
            p1 = p1.next = null;
            p0 = p0.next = null;
            return head;
        }
    }
    

    I am a novice in Java, so there should be a way to clean the above code a bit...


Log in to reply
 

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