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 ...
随机推荐
- Ubuntu 18.04 Python3.6.6导入wx模块报Gtk-Message : 17:06:05.797 :Failed to load module "canberra-gtk-module"
解决办法: root@sishen:~# apt-get install libcanberra-gtk-module
- Linux之shell命令实现-批量去掉文件名中空格,以及批量修改文件名为数字序号文件名
1 shell下批量出去文件名中的空格 执行看现象: 上面的是执行for循环以后看到的: 然而源目录下的文件如下: 这样的话想要cat某个具体文件是拿不到的,所以需要去空格处理: 处理方式有很多:如 ...
- MVC 路由URL重写
在现今搜索引擎制霸天下的时代,我们不得不做一些东西来讨好爬虫,进而提示网站的排名来博得一个看得过去的流量. URL重写与优化就是搜索引擎优化的手段之一. 假如某手机网站(基于ASP.NET MVC)分 ...
- SQL server下所有表名及字段名及注释查询
--查询所有表及注释SELECTA.name ,C.valueFROM sys.tables A LEFT JOIN sys.extended_properties C ON C.major_id = ...
- 滚动条美化插件 nicescroll
这个插件使用起来非常简单,首先下载插件jquery.nicescroll.min.js 然后在页面中引入jquery,再引入这个插件, 然后在页面中需要修改滚动条的地方,例如id为box的div &l ...
- 1126 数字统计 2010年NOIP全国联赛普及组
1126 数字统计 2010年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 请统计某个 ...
- C语言中的static和extern
c语言中,全局变量是一个非常重要的概念.全局变量定义在函数外,可以被所有的函数共同使用. #include <iostream> ; void display() { printf(&qu ...
- hadoop的shell总结
ls命令(文件内容) 列出系统跟目录下的目录和文件 Hadoop fs -ls / 列出文件系统所有的目录和文件 Hadoop fs -ls -R / cat命令(列出文档内容) Hadoop fs ...
- Redis相关注意事项
本文介绍了五个使用Redis使用时的注意事项.如果你在使用或者考虑使用Redis,你可以学习一下下面的一些建议,避免遇到以下提到的问题. 一.配置相关注意事项 1.涉及到内存的单位注意添加 b 1k ...
- [Git]使用Git上传本地项目,并同步到Github上
第一步:先要在github.com中创建一个仓库(New Repository). 第二步,打开Git Bash ① git init [+仓库名]:初始化仓库,执行之后可以在指定的仓库存放地上面看到 ...