Can anybody help me with my CPP solution


  • 0
    J
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* oddEvenList(ListNode* head) {
            if(!head || !head->next)
    			return head;
    		ListNode* Oddhead=head;
    		ListNode* Evenhead=head->next;
    		ListNode* Odd=Oddhead;
    		ListNode* Even=Evenhead;
    		int index=1;
    		head=head->next->next;
    		while(head!=NULL)
    		{
    			if(index%2==0)
    			{
    				Even->next=head;				
    				Even=Even->next;
    			}
    			else if(index%2==1)
    			{		
    				Odd->next=head;
    				Odd=Odd->next;
    			}
    			head=head->next;
    			index++;
    		}
    		Odd->next=Evenhead;
    		return Oddhead;
        }
    };
    

    where is wrong? I can't really figure out why the case [1,2 ,3] cannot pass. If anyone can help me , thanks a lot.


Log in to reply
 

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