java easy understand


  • 0
    J
    public class Solution {
        public ListNode oddEvenList(ListNode head) {
            int count = 1;
            ListNode tail = null;
            ListNode cur = head;
            ListNode pre = head;
            ListNode even = null;
            while(cur != null){
                if(cur.next == null) tail = cur;
                cur = cur.next;
            }
            cur = head;
            while(cur != null && cur != even){
                if(count % 2 == 0 && cur.next != null){
                        ListNode temp = cur.next;
                        pre.next = temp;
                        tail.next = cur;
                        cur.next = null;
                        tail = tail.next;
                        if(count == 2) even = tail;
                        cur = temp;
                        count++;
                }else{
                    pre = cur;
                    cur = cur.next;
                    count++;
                }
            }
            return head;
        }
    }

Log in to reply
 

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