力扣算法题—143ReorderList
Given a singly linked list L : L 0→ L 1→…→ L n -1→ L n, reorder it to: L 0→ L n → L 1→ L n -1→ L 2→ L n -2→… You may not modify the values in the list's nodes, only nodes itself may be changed. Example 1: Given 1->2->3->4, reorder it to 1->4->2->3. Example 2: Given 1->2->3->4->5, reorder it to 1->5->2->4->3.Solution: 使用快慢指针,得到后半部分的链表 使用栈存储后半部分的链表来替代翻转链表 1 class Solution { 2 public: 3 void reorderList(ListNode* head) { 4 if (head == nullptr || head->next == nullptr)return; 5 ListNode* slow = head, *fast = head; 6 while (fast && fast->next) 7 { 8 slow = slow->next; 9 fast = fast->next->next;