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 ...
随机推荐
- ABP框架和NET CORE实战
http://www.fishpro.com.cn/2017/09/ ABP实战系列 ABP实战 ABP-第一个Asp.net core 示例(7)AutoMapper的使用 我们为什么需要使用DDD ...
- 爬虫(Xpath)——爬tieba.baidu.com
工具:python3 核心知识点: 1)lxml包不能用pip下载,因为里面有其他语言编写的文件 2)urlopen返回的请求是html文件,要使用 content = etree.HTML(html ...
- spoolight on oracle 配置
spoolight seting 1ORACLE_HOME=D:\oracle\product\11.2.0\client_1set SQLPATH=D:\oracle\product\11.2.0\ ...
- C# 序列化与反序列化json
与合作伙伴讨论问题,说到的c++与c#数据的转换调用,正好就说到了序列化与反序列化,同样也可用于不同语言间的调用,做了基础示例,作以下整理: using System.Data; using Syst ...
- 对jvm虚拟机 内存溢出的思考
java内存溢出:当新产生对象时,新生代空间不够,导致无法申请到足够的空间,报内存溢出 内存泄漏:一些静态集合,静态常量没有被gc清理,越来越多,占用内存,最后导致无法申请到新的空间
- JavaWeb项目开发中eclipse缓存问题
学习Java快2年了 有时候改完代码启动tomcat测试时,新代码不生效,这可能就是缓存问题. 所以平时就用以下几个方法解决,如果还是解决不了,就找老师吧! 1.清理项目 2.移除项目,清理tomca ...
- w3c万维网的介绍和html基本构成
怎么与浏览器交互? 1.鼠标 2.键盘输入 w3c标准: 中文名:万维网联盟!外文名:world wide web cansortium万维网联盟创建于1994年,是web技术领域最具权威个影响的国际 ...
- html学习笔记-XML-Javascript
html学习笔记-XML-Javascript Table of Contents 1. XML HTTP Request 1.1. XMLHttpRequest 对象 1.2. 创建 XMLHttp ...
- POJ 1651 Multiplication Puzzle (区间DP,经典)
题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...
- idea存留
1.导师匿名评价系统 类似看准网 2.在线降重软件 基于翻译api,或者后期写算法 在线查重导流 公众号: e搜罗