您的位置:首页 > 娱乐 > 八卦 > 家居装饰网站设计论文_个人简历制作_自己做网站_win7优化工具

家居装饰网站设计论文_个人简历制作_自己做网站_win7优化工具

2025/5/12 10:26:15 来源:https://blog.csdn.net/weixin_41554427/article/details/147591119  浏览:    关键词:家居装饰网站设计论文_个人简历制作_自己做网站_win7优化工具
家居装饰网站设计论文_个人简历制作_自己做网站_win7优化工具

题目描述

容易想到的做法是,把后半部分链表反转后的链表和原来的前半部分链表合并,即可得到想要的结果。需要先找链表的中间结点,参考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;
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com