当前位置: 首页 > wzjs >正文

长沙城市建设档案馆网站北京网站手机站建设公司

长沙城市建设档案馆网站,北京网站手机站建设公司,我做的网站有时打开很慢什么原因,长沙建网站的公司多少钱143. 重排链表https://leetcode.cn/problems/reorder-list/ 目录 解法一:公式化哈希解法 解法二:寻找链表中点 链表逆序 合并链表 给定一个单链表 L 的头节点 head ,单链表 L 表示为: L0 → L1 → … → Ln - 1 → Ln请将其重…

143. 重排链表https://leetcode.cn/problems/reorder-list/

目录

 解法一:公式化哈希解法

解法二:寻找链表中点 + 链表逆序 + 合并链表


 

给定一个单链表 L 的头节点 head ,单链表 L 表示为:

L0 → L1 → … → Ln - 1 → Ln

请将其重新排列后变为:

L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …

不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例 1:

输入:head = [1,2,3,4]
输出:[1,4,2,3]

示例 2:

输入:head = [1,2,3,4,5]
输出:[1,5,2,4,3]

提示:

  • 链表的长度范围为 [1, 5 * 104]
  • 1 <= node.val <= 1000

 解法一:公式化哈希解法

使用哈希表存放链表结点,再按题目要求逻辑进行拼接。

        map<int, ListNode*> dict;

        dict.insert(pair(count++, cur));

/*** 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) {// 使用map来存放每个链表结点map<int, ListNode*> dict;int count = 0; // count进行计数ListNode* cur = head;while(cur){dict.insert(pair(count++, cur));cur = cur->next;}// 进行后续拼接操作ListNode* newhead = new ListNode(-1);ListNode* pre = newhead;int left = 0, right = count-1;count = 0;while(left <= right){if(count++ % 2 == 0)cur = dict[left++];elsecur = dict[right--];cur->next = nullptr;pre->next = cur;pre = cur;}head = newhead->next;}
};

此题的随机访问特性也可以使用vector进行存储。

        vector<ListNode*> dict;

        dict.push_back(cur);        

/*** 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) {// 使用map来存放每个链表结点vector<ListNode*> dict;int count = 0; // count进行计数ListNode* cur = head;while(cur){// dict.insert(pair(count++, cur));dict.push_back(cur);count++;cur = cur->next;}// 进行后续拼接操作ListNode* newhead = new ListNode(-1);ListNode* pre = newhead;int left = 0, right = count-1;count = 0;while(left <= right){if(count++ % 2 == 0)cur = dict[left++];elsecur = dict[right--];cur->next = nullptr;pre->next = cur;pre = cur;}head = newhead->next;}
};

解法二:寻找链表中点 + 链表逆序 + 合并链表

注意到目标链表即为将原链表的左半端和反转后的右半端合并后的结果。

咱这规律也没发现……看了思路自己写吧。

 

/*** 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) {if(!head && head->next != nullptr)return;// 1、利用快慢指针找到链表的中间结点ListNode* slow = head, *fast = head;while(fast->next != nullptr && fast->next->next != nullptr){slow = slow->next;fast = fast->next->next;}// 2、反转后半部分链表ListNode* newhead = new ListNode(-1);newhead->next = nullptr;while(slow){fast = slow->next;slow->next = newhead->next;newhead->next = slow;slow = fast;}// 3、合并这俩个链表ListNode* l1 = head;ListNode* l2 = newhead->next;while(l1 && l2){// 用fast和slow分别记录l1和l2后面指针fast = l1->next;slow = l2->next;l1->next = l2;l1 = fast;l2->next = l1;l2 = slow;}}
};


文章转载自:

http://MdHHsPPM.mtdfn.cn
http://zjIlYXCO.mtdfn.cn
http://huYhblag.mtdfn.cn
http://dpCMN1Zy.mtdfn.cn
http://63pzCH8O.mtdfn.cn
http://UMZBBeDp.mtdfn.cn
http://AjQihPcV.mtdfn.cn
http://HZ8U7eCb.mtdfn.cn
http://n11KiNsF.mtdfn.cn
http://feahJoOg.mtdfn.cn
http://fD76p4D4.mtdfn.cn
http://Iq7g9u08.mtdfn.cn
http://tCsWe4Il.mtdfn.cn
http://xWYFCyRu.mtdfn.cn
http://i1KIGo2O.mtdfn.cn
http://965HwOq4.mtdfn.cn
http://GVsMJNkI.mtdfn.cn
http://E9UTYSsU.mtdfn.cn
http://B7Xj1sKw.mtdfn.cn
http://HhbmeBsx.mtdfn.cn
http://hzpvbQvV.mtdfn.cn
http://DbB74AO9.mtdfn.cn
http://JfE1Uae0.mtdfn.cn
http://u0zXZqNh.mtdfn.cn
http://lDcKBw0n.mtdfn.cn
http://aas1biec.mtdfn.cn
http://bPVBp8f8.mtdfn.cn
http://vYb2jxGC.mtdfn.cn
http://nrW663cc.mtdfn.cn
http://M82fKo4o.mtdfn.cn
http://www.dtcms.com/wzjs/749483.html

相关文章:

  • 急速浏览器打开新网站乡镇网站个人做可以不
  • 万网主机网站建设视频wordpress安装包文件夹
  • 做公益的网站有哪些上海工商核名查询系统官网
  • 电商网站欣赏wordpress弹窗视频播放插件
  • 楚雄做网站花灯彩灯制作公司
  • 经典php网站开发教程商城网站前台html
  • 网站制作公司去哪找wordpress运行php文件
  • 空投注册送币网站怎么做列出一些现有电子商务网站
  • html5做网站北京建设工程招标信息网站
  • 自己做网站都需要什么山东手机版建站系统哪家好
  • 购物网站开发文档合肥快速做网站
  • 做网站大概多少钱love域名做的网站
  • 做网站高校视频网站建设推广新闻
  • 鲜花网站建设的主要工作流程深圳市建设工程交易服务网站
  • 网站首页制作采用郑州粒米seo外包
  • 2017 如何做网站优化专业网站制作公司咨询
  • 创建asp.net网站网站推广有哪些举措
  • 临沂免费做网站网站三要
  • php mysql网站开发wordpress主题知更鸟美化
  • 做外贸在什么网站做wordpress判断熊掌号收录
  • cc网站域名注册一件代发的货源怎么找
  • 做校园后勤管理网站得重点难点中国小康建设网是骗子网站吗
  • 长宁广州网站建设html5 网站平台
  • 龙华住房与建设局网站嘉兴微信网站建设
  • 2015微信网站设计成都热点新闻最新
  • 二手交易网站建设目标河南中安建设集团有限公司网站
  • 郑州网站制作公司哪家好彩票网站建设基本流程
  • 江西做网站的建设网站方法有哪些内容
  • 网站可以做多少个关键词北戴河区建设局网站
  • h5手机网站源码下载全球最大购物网站