LeetCode(143) 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}.
分析
如题所示,要求将给定链表的前半部分和后半部分交叉链接。
方法一:(解决问题但是TLE)
采用的方法是将前面的节点逐个与当前尾节点链接,当然,每次都需要求取当前尾节点,这就造成了平方的复杂度。
方法二:
首先,可以将给定链表一分为二,然后合并。
AC代码
class Solution {
public:
//方法一,逐个交换
void reorderList1(ListNode* head) {
if (!head || !head->next)
return ;
//逐个节点元素值交换
ListNode *p = head, *pre = head , *q = head;
while (p)
{
//只剩下一个尾节点
if (q->next == NULL)
return;
//寻找当前末尾节点
while (q->next->next)
{
q = q->next;
}
//保存末尾节点的前一个节点
pre = q;
//得到末尾节点
q = q->next;
//处理完毕
if (p == pre)
return;
//改变链接
q->next = p->next;
p->next = q;
//新末尾节点后继置空
pre->next = NULL;
p = q->next;
q = p;
}
return;
}
void reorderList(ListNode* head) {
//空链表或单节点或双节点链表直接返回
if (!head || !head->next || !head->next->next)
return;
/* 先用快慢指针找到链表的中点,然后翻转链表后半部分,再和前半部分组合。
* 需要注意的是把链表分成两半时,前半段的尾节点要置为NULL,翻转链表时也要把尾节点置为NULL。
*/
ListNode *slow = head, *fast = head;
//把整个链表划分成2个等长的子链表,如果原链表长度为奇数,那么第一个子链表的长度多1
while (fast->next != NULL) {
fast = fast->next;
if (fast->next != NULL)
fast = fast->next;
else
break;
slow = slow->next;
}
ListNode *f_head = head, *s_head = slow->next;
//将前半部分链表尾节点链接到空
slow->next = NULL;
//翻转第二个链表
ListNode *p = s_head, *q = s_head->next;
p->next = NULL;
while (q)
{
ListNode *r = q->next;
q->next = p;
p = q;
q = r;
}
s_head = p;
//合并两个链表
p = f_head, q = s_head;
while (q)
{
//保存两个子链表下一节点
ListNode *f_r = p->next , *s_r = q->next;
p->next = q;
q->next = f_r;
p = f_r;
q = s_r;
}//while
}
};
LeetCode(143) Reorder List的更多相关文章
- 新概念英语(1-43)Hurry up!
新概念英语(1-43)Hurry up! How do you know Sam doesn't make the tea very often? A:Can you make the tea, Sa ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- java实现xml文件读取并保存到对象
首先浅聊一下解析xml的四种方式: 1.DOM方式:有缺点但是这个缺点却也是他的优点.下面详细介绍: 以树形的层次结构组织节点或信息片断集合,可以获得同一个文档中的多处不同数据.使用起来简单. 优点是 ...
- datatables后台分页例子(可直接复制代码)
1.head表签引用 这两个文件即可 2.复制下面的代码到webform中的head标签中 <script> $(function () { //提示信息 var lang = { &qu ...
- 使用javaMail发送简单邮件
参考网页:http://blog.csdn.net/xietansheng/article/details/51673073package com.bfd.ftp.utils; import java ...
- git上传布置代码 git优势
ftp 软件 可直接上传至服务器但不便于管理 Git上传 GitHub/码云/codinghub 登录服务器 ssh 协议登录 ssh 账户@ip地址 密码 mkdir 创建文件 workspace ...
- (2017.10.16) javascript 数据类型转换与操作
javascript 有 5 种基本数据类型:undefined.null.Boolean.String.Number,还有1 种较复杂的数据类型 Object:各种类型之间可以相互转换,其中有些有趣 ...
- PADS 创建封装笔记
1.在PADS logic中新建元件和CAE封装 2.在PADS layout 中建立元件的PCB封装 3.用PADS Library Converter 把以前版本的库转化为现在的版本.
- 如何在Android Studio中导入JNI生成的.so库
由于在原来的ADT的Eclipse环境中,用ndk_build工具生成了相应的各个.so库文件之后,eclipse工具就会自动把这些库导入到apk中.而Android Studio目前为止(0.86版 ...
- C# 报表和打印等
说到报表打印.那就不得不说需要查数据库了,然后填写报表信息.设计报表用的 grid++. 查数据库时候,我也是醉了,直接一个表自身与自身级联了4次...一共取了7个表的信息数据. 关于级联--(表字段 ...
- Vue风格指南总结及对应ESLint规则配置
全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/10906951.html,多谢,=.=~ 必要的:规避错误: 强烈推荐:改善可读性和开发体验: 推 ...
- 【Python图像特征的音乐序列生成】使用Python生成简单的MIDI文件
这个全新的Python音乐创作系列,将会不定期更新.写作这个系列的初衷,是为了做一个项目<基于图像特征的音乐序列生成模型>,实时地提取照片特征,进行神经网络处理,生成一段音乐. 千里之行, ...