LeetCode | Reorder List

题目:

Given a singly linked list LL0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.


思路:

利用快慢两个指针将链表一分为二,针对第二个子链表求倒序,最后将两个子链表合并。


代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode *head) {
        ListNode * slow=head;
        ListNode * fast=head;
        
        if(head != NULL && head->next != NULL)
        {
            while(fast!=NULL && fast->next != NULL)
            {
                slow=slow->next;
                fast=fast->next->next;
            }
            
            ListNode * tmp = slow->next;
            slow->next = NULL;
            ListNode * p2= head;
            ListNode * p1 =NULL;
            while(tmp!=NULL)
            {
                ListNode* t = p1;
                p1=tmp;
                tmp=tmp->next;
                p1->next = t;
            } 
            tmp =NULL;
            while(p1!=NULL && p2!=NULL)
            {
                if(tmp==NULL)
                {
                    tmp=p2;
                }
                else
                {
                    tmp->next = p2;
                    tmp=tmp->next;
                }
                p2=p2->next;
                tmp->next =p1;
                p1=p1->next;
                tmp=tmp->next;
                tmp->next = NULL;
            }
            
            if(p1!=NULL)
            {
                if(tmp==NULL)
                {
                    tmp=p1;
                    
                }
                else
                {
                    tmp->next=p1;
                }
            }
            
            if(p2!=NULL)
            {
                if(tmp==NULL)
                {
                    tmp=p2;
                }
                else
                {
                    tmp->next=p2;
                }
            }
        }
    }
};

重新优化之后
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode *head) {
        if(head == NULL){
            return;
        }
        
        ListNode* p1 = head;
        ListNode* p2 = splitList(head);
        
        p2 = revertList(p2);
        
        mergeList(p1, p2);
    }
    
    void mergeList(ListNode * p1, ListNode * p2){
        while(p2 != NULL){
            ListNode* tmp = p2;
            p2 = p2->next;
            
            tmp->next = p1->next;
            p1->next = tmp;
            p1 = p1->next;
            p1 = p1->next;
        };
    }
    
    ListNode* splitList(ListNode *head){
        ListNode* slow = new ListNode(0);
        slow->next = head;
        ListNode* fast = slow;
        
        while(fast->next != NULL && fast->next->next != NULL){
            slow = slow->next;
            fast = fast->next;
            fast = fast->next;
        }
        
        if(fast->next != NULL){
            slow = slow->next;
            fast = fast->next;
        }
        
        ListNode* tmp = slow->next;
        slow->next = NULL;
        return tmp;
    }
    
    ListNode* revertList(ListNode* head){
        if(head == NULL){
            return NULL;
        }
        
        ListNode* p = head->next;
        head->next = NULL;
        
        while(p != NULL){
            ListNode* tmp = p;
            p = p->next;
            tmp->next = head;
            head = tmp;
        }
        return head;
    }
};


你可能感兴趣的:(LeetCode,LeetCode题解)