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 exception "file not found or file not exist"
出现这种异常一般有两种原因,第一种就是文件真的不存在:第二种是权限问题,权限问题又分为文件本身的权限和包含它的文件夹的权限 比如 ~/aaa/bbb/ccc/ddd/eee.txt 只要 aaa , ...
- 配置本地和远程maven仓库
<mirrors><mirror> <id>alimaven</id> <name>aliyun maven</name> &l ...
- Typora--Draw Diagrams With Markdown
Typora Typora supports some Markdown extension for diagrams, you could enable this feature from pref ...
- I/O————对象流
对象流指的是可以直接把一个对象以流的形式传输给其他的介质,比如硬盘 一个对象以流的形式进行传输,叫做序列化. 该对象所对应的类,必须是实现Serializable接口 对象的序列化与反序列化就是从文件 ...
- 客户端rsyslog配置文件详解
客户端rsyslog配置文件详解 最近再开发一个rsyslog的接收服务端,支持udp,tcp和tls三种协议.所以去仔细研究了一下rsyslog.conf的配置文件,下面来详细说一下. 因为我这儿重 ...
- HBuilder 做移动端app流程
1.新建一个移动项目 2.编写代码 3.发行-发行为原生安装包,配置参数 选择icon 和引导页
- js基础的自定义属性练习
js基础的自定义属性练习: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type ...
- 织梦channel标签中currentstyle不生效
文件:/include/taglib/channel.lib.php line约133行:if( ($row['id']==$typeid || ($topid==$row['id'] &&a ...
- C语言的sprintf()和snprintf()
1.sprintf()函数 送格式化输出到字符串中,返回实际输出到字符串中的个数. 例如: char buffer[80]; sprint(buffer,"1234567890") ...
- C#的位运算
链接地址: http://www.cnblogs.com/NetBelieve/archive/2012/07/30/2615006.html