Leetcode 328 Odd Even Linked List

这道题是merge two linked list的逆过程,即就是先要分成两个linked list,然后合并到一起即可。

class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        if (head == NULL || head->next == NULL) {
            return head;
        }
        
        ListNode* first = head;
        ListNode* second = head->next;
        ListNode* reserve_second = second;
        
        while(second && second->next) {
            first->next = second->next;
            first = first->next;
            second->next = first->next;
            second = second->next;
        }
        
        first->next = reserve_second;
        return head;
    }
};

 

posted @ 2018-09-04 21:29  ly-bnu  阅读(78)  评论(0编辑  收藏  举报