leetcode 143. Reorder List

Posted ymjyqsx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 143. Reorder List相关的知识,希望对你有一定的参考价值。

143. Reorder List

https://www.cnblogs.com/grandyang/p/4254860.html

先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接

class Solution {
public:
    void reorderList(ListNode* head) {
        if(head == NULL)
            return;
        ListNode* p1 = head;
        ListNode* p2 = head;
        while(p2->next != NULL && p2->next->next != NULL){
            p1 = p1->next;
            p2 = p2->next->next;
        }
        p2 = p1->next;
        p1->next = NULL;
        ListNode* pre = NULL;
        while(p2 != NULL){
            ListNode* tmp = p2->next;
            p2->next = pre;
            pre = p2;
            p2 = tmp;
        }
        p1 = head;
        p2 = pre;
        while(p2 != NULL){
            ListNode* tmp1 = p1->next;
            ListNode* tmp2 = p2->next;
            p1->next = p2;
            p2->next = tmp1;
            p1 = tmp1;
            p2 = tmp2;
        }
        return;
    }
};

 

以上是关于leetcode 143. Reorder List的主要内容,如果未能解决你的问题,请参考以下文章

leetcode143- Reorder List问题

143. Reorder List - LeetCode

LeetCode 143. Reorder List

Leetcode 143. Reorder List

LeetCode-143-Reorder List

LeetCode 143. Reorder List