leetcode 143. Reorder List

来源:互联网 发布:广州造价数据 编辑:程序博客网 时间:2024/06/14 06:12
class Solution {public:void reorderList(ListNode* head){if (head == nullptr || head->next == nullptr){return;}auto p1 = head, p2 = head->next;while (p2 && p2->next){p1 = p1->next;p2 = p2->next->next;}auto head2 = p1->next;p1->next = nullptr;p2 = head2->next;head2->next = nullptr;while (p2){p1 = p2->next;p2->next = head2;head2 = p2;p2 = p1;}p1 = head;p2 = head2;while (p1){auto temp = p1->next;p1->next = p2;p1 = p1->next;p2 = temp;}}};

0 0
原创粉丝点击