leetcode 143. 重排链表
题目描述
容易想到的做法是,把后半部分链表反转后的链表和原来的前半部分链表合并,即可得到想要的结果。需要先找链表的中间结点,参考leetcode 876. 链表的中间结点-CSDN博客
反转链表可以用正统的反转链表的方法(leetcode官方即用的此法),也可以用栈来实现。
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:void reorderList(ListNode* head) {ListNode* slow = head;ListNode* fast = head;while(fast != nullptr && fast->next != nullptr){fast = fast->next->next;slow = slow->next;}stack<ListNode*> node_stack;ListNode* back_half = slow;while(back_half != nullptr){node_stack.push(back_half);back_half = back_half->next;}ListNode* middle = slow;ListNode* ans = new ListNode(0,head);ListNode* tail = nullptr;ListNode* front_half = head;while(front_half != middle && !node_stack.empty()){if(tail == nullptr){tail = front_half;ans->next = tail;}else{tail->next = front_half;tail = tail->next;}front_half = front_half->next;tail->next = node_stack.top();tail = tail->next;node_stack.pop();}while(front_half != middle){if(tail == nullptr){tail = front_half;ans->next = tail;}else{tail->next = front_half;tail = tail->next;}front_half = front_half->next;}while(!node_stack.empty()){if(tail == nullptr){tail = node_stack.top();ans->next = tail;}else{tail->next = node_stack.top();tail = tail->next;}node_stack.pop();}tail->next = nullptr;head = ans->next;delete ans;}
};
如第65行所示,最后一定要把tail->next置为nullptr,不然结果链表中会出现环路。
测试代码:
#include<iostream>
#include<stack>
using namespace std;// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}ListNode(int x, ListNode *next) : val(x), next(next) {}};class Solution {
public:void reorderList(ListNode* head) {ListNode* slow = head;ListNode* fast = head;while(fast != nullptr && fast->next != nullptr){fast = fast->next->next;slow = slow->next;}stack<ListNode*> node_stack;ListNode* back_half = slow;while(back_half != nullptr){node_stack.push(back_half);back_half = back_half->next;}cout<<"node_stack.size():"<<node_stack.size()<<endl;ListNode* middle = slow;ListNode* ans = new ListNode(0,head);ListNode* tail = nullptr;ListNode* front_half = head;while(front_half != middle && !node_stack.empty()){if(tail == nullptr){tail = front_half;ans->next = tail;}else{tail->next = front_half;tail = tail->next;}front_half = front_half->next;tail->next = node_stack.top();tail = tail->next;node_stack.pop();}while(front_half != middle){if(tail == nullptr){tail = front_half;ans->next = tail;}else{tail->next = front_half;tail = tail->next;}front_half = front_half->next;}while(!node_stack.empty()){if(tail == nullptr){tail = node_stack.top();ans->next = tail;}else{tail->next = node_stack.top();tail = tail->next;}node_stack.pop();}tail->next = nullptr;head = ans->next;delete ans;}
};int main()
{ListNode* dummy = new ListNode();ListNode* cur = nullptr;for(int i = 1; i<=5;i++){ListNode* pNode = new ListNode(i);if(cur == nullptr){cur = pNode;dummy->next = cur;}else{cur->next = pNode;cur = cur->next;}}cur = dummy->next;while(cur){cout<<cur->val<<",";cur = cur->next;}cout<<endl;Solution so;so.reorderList(dummy->next);cur = dummy->next;while(cur){cout<<cur->val<<",";cur = cur->next;}cout<<endl;return 0;
}