328. Odd Even Linked List

问题描述

解决方案

/**
 * 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* oddlist=head;
        ListNode* evenlist=head->next; 
        ListNode* node=head->next;
        while(evenlist)
        {
            oddlist->next=evenlist->next;
            if(oddlist->next!=NULL)
            {
                oddlist=oddlist->next;
                evenlist->next=oddlist->next;
            }
            evenlist=evenlist->next;
        }
        oddlist->next=node;
        return head;
    }
};
posted @ 2016-08-25 00:02  弦断  阅读(171)  评论(0编辑  收藏  举报