How to make this code go through judging?


  • 0
    Z

    public class Solution {
    public ListNode oddEvenList(ListNode head) {

        if(head == null) return null;
        ListNode odd = head;
        ListNode oddLast = head; 
        if(head.next == null) return head;
        ListNode even = head.next;
        ListNode evenLast = even;
        head = head.next;
        int i = 0;
        while(head.next !=null){
            head = head.next;
            i++;
            if(i%2 == 1){
                oddLast.next = head;
                oddLast =head;
            }
            else{
                evenLast.next = head;
                evenLast = head;
            } 
        }
        oddLast.next = even;
        evenLast = null;
        return odd;
    }
    

    }


Log in to reply
 

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