LeetCode每日一题 143. 重排链表

it2022-12-27  74

题目链接

思路

双指针

class Solution { public: void reorderList(ListNode* head) { vector<ListNode*> v; ListNode* tmp = head; while (tmp) { v.push_back(tmp); tmp = tmp->next; } int l = 0, r = v.size() - 1; while (l < r-1) { v[l]->next = v[r]; v[r]->next = v[l+1]; v[r-1]->next = nullptr; l++; r--; } } };
最新回复(0)