本文主要是介绍Leetcode154: Reorder List,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-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) {if (head == nullptr) return; vector<ListNode*> nodes; ListNode* iter = head; while(iter != nullptr) { nodes.push_back(iter); iter = iter->next; } int LEN = nodes.size(); int left = 0; int right = LEN -1; while(left < right) { nodes[left]->next = nodes[right]; nodes[right--]->next = nodes[++left]; } nodes[left]->next = nullptr; }
};
这篇关于Leetcode154: Reorder List的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!