题解 | #重排链表#

重排链表

https://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b

/**
 * 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==nullptr||!head->next||!head->next->next)return;
        ListNode* h=head;ListNode* p=head;
        while(h->next->next)h=h->next;
        ListNode* t=h->next;
        h->next=nullptr;
        t->next=p->next;p->next=t;
        reorderList(t->next);
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务