【链表】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}
.
思路:
先用快慢指针找到链表的中点,然后翻转链表后半部分,再和前半部分组合。需要注意的是把链表分成两半时,前半段的尾节点要置为NULL,翻转链表时也要把尾节点置为NULL。
注意:后半部分可能比前半部分长,所以最后要判断一下是否将后半部分全部加入链表。
- /**
- * Definition for singly-linked list.
- * function ListNode(val) {
- * this.val = val;
- * this.next = null;
- * }
- */
- /**
- * @param {ListNode} head
- * @return {void} Do not return anything, modify head in-place instead.
- */
- var reorderList = function(head) {
- if(head==null||head.next==null){
- return;
- }
- var l=head,r=head,lpre=null;
- while(r!=null&&r.next!=null){
- lpre=l;
- l=l.next;
- r=r.next.next;
- }
- lpre.next=null;
- r=reverseList(l);
- var p=head,temp=null,tempr,tail=null;
- while(p!=null){
- temp=p.next;
- tempr=r;
- r=r.next;
- p.next=tempr;
- tail=p.next;
- tempr.next=temp;
- p=temp;
- }
- if(r!=null){
- tail.next=r;
- }
- };
- var reverseList = function(head) {
- if(head==null||head.next==null){
- return head;
- }
- var temp=null,pre=null,cur=null;
- pre=head;
- cur=pre.next;
- pre.next=null;
- while(cur!=null){
- temp=cur.next;
- cur.next=pre;
- pre=cur;
- cur=temp;
- }
- return pre;
- };
【链表】Reorder List的更多相关文章
- [Swift]LeetCode143. 重排链表 | 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 may not mod ...
- LeetCode_算法及数据结构覆盖统计
[输入]共计151道题的算法&数据结构基础数据 (见附录A) [输出-算法]其中有算法记录的共计 97道 ,统计后 结果如下 top3(递归,动态规划,回溯) 递归 动态规划 回溯 BFS ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode总结【转】
转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetco ...
- leetcode解题文件夹
点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...
- [LeetCode] 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 th ...
- 62. 链表重排[Reorder List]
[本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...
- LeetCode之“链表”: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 ...
- 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 thi ...
- 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...
链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...
随机推荐
- 前端程序员经常忽视的一个 JavaScript 面试题
题目 function Foo() { getName = function () { alert (1); }; return this; } Foo.getName = function () { ...
- oss上传文件夹-cloud2-泽优软件
泽优软件云存储上传控件(cloud2)支持上传整个文件夹,并在云空间中保留文件夹的层级结构,同时在数据库中也写入层级结构信息.文件与文件夹层级结构关系通过id,pid字段关联. 本地文件夹结构 文件 ...
- 配置 cxf-ws spring bean 文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 树和二叉树在java中
树代表一种非线性的数据结构,如果一组数组节点之间存在复杂的一对多关联时,程序就可以考虑使用树来保存这组数据了. 线性表.栈和队列都是线性的数据结构,这种数据结构之内的元素只存在一个对一个的关系.存储, ...
- Chinese Seals
Emperors used seals to proclaim their decrees to their people, officials used seals to exercise thei ...
- 集合(二)LinkedList
上一篇中讲解了ArrayList,本篇文章讲解一下LinkedList的实现. LinkedList是基于链表实现的,所以先讲解一下什么是链表.链表原先是C/C++的概念,是一种线性的存储结构,意思是 ...
- [Linux-vi] The simple set of vi command
Source : https://www.cs.colostate.edu/helpdocs/vi.html What is vi? The default editor that comes wit ...
- lua的table的删除操作
直接使用table的remove方法 , , } table.remove(tab) --默认删除最后一个元素,第二个参数可以指定删除位置 删除后,后面的元素会往前移动.有点像C++的std::vec ...
- ECG心电图数据2
1.如何下载获取MIT-BIH的数据从下面这个官方链接页面可以下载到所有48组MIT-BIH心电数据: http://www.physionet.org/physiobank/database/mit ...
- Sublime Text 3 格式化HTML CSS JS 代码
一,首先通过ctrl+shift+p 要等一会就会出现插件安装界面 二,在插件安装输入框,输入:HTML-CSS-JS Prettify 并安装该插件 三,如果没有装nodejs, 下载nodejs ...