A very easy to understand C solution


  • 0
    S
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* oddEvenList(struct ListNode* head) {
        if(head == NULL)
        {
            return NULL;
        }
        struct ListNode* headodd;
        struct ListNode* headodd_dummy;
        struct ListNode* headeven;
        struct ListNode* headeven_dummy;
        headodd = head;
        headodd_dummy = head;
        if(head->next == NULL) //only one node
        {
            return head;
        } else
        {
            headeven = head->next;
            headeven_dummy = head->next;
        }
        
        while(headodd->next!=NULL && headeven->next!=NULL)
        {
            headodd->next = headeven->next;
            headodd = headeven->next;
            headeven->next = headodd->next;
            headeven = headodd->next;
        }
        
        headodd->next = headeven_dummy;
        
        return headodd_dummy;
         
    }
    

Log in to reply
 

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