[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}
.
看上去有点难,实际上可以分为几个步骤。
1.找到中间节点。划分为两个链表。(算法总结里有)
2.把后半部分链表反转。(算法总结里有)
3.合并两个链表。(递归)
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* ReverseList(ListNode* head){ 12 if(head==NULL||head->next==NULL) 13 return head; 14 ListNode* p=head->next,*q=head,*l=head; 15 while(p->next!=NULL){ 16 l=p->next; 17 p->next=q; 18 q=p; 19 p=l; 20 } 21 p->next=q; 22 head->next=NULL; 23 return p; 24 } 25 ListNode* Merge(ListNode* l1,ListNode* l2){ 26 if(l1==NULL) 27 return l2; 28 if(l2==NULL){ 29 l1->next=NULL; 30 return l1; 31 } 32 ListNode* p=l1,*q=l2; 33 l1=l1->next; 34 l2=l2->next; 35 p->next=q; 36 q->next=Merge(l1,l2); 37 return p; 38 } 39 void reorderList(ListNode* head) { 40 if(head==NULL||head->next==NULL) 41 return; 42 ListNode* p=head,*q=head,*bf=NULL; 43 while(p&&p->next!=NULL){ 44 p=p->next->next; 45 bf=q; 46 q=q->next; 47 } 48 bf->next=NULL; 49 q=ReverseList(q); 50 Merge(head,q); 51 return; 52 } 53 };
转载于:https://www.cnblogs.com/LUO77/p/5672007.html
[leetcode]143. Reorder List相关推荐
- 【重点】LeetCode 143. Reorder List
LeetCode 143. Reorder List Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4254860.html 这段代码有值得学习 ...
- [LeetCode] 143. Reorder List_Middle tag: Linked 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 mo ...
- 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 may not mo ...
- LeetCode 143. Reorder List
Tag:List Difficulty:Medium Problem 重排链表 给定一个单链表 L 的头节点 head ,单链表 L 表示为: L0 → L1 → - → Ln - 1 → Ln ...
- 143. Reorder List
/** 143. Reorder List * 11.28 by Mingyang 总体思想就是后半部分reverse然后再merge*/public void reorderList(ListNod ...
- 【Leetcode】143. Reorder List
Question: Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You ...
- LeetCode 143. 重排链表(Reorder List)
题目描述 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. ...
- 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 ...
- Leetcode 143. 重排链表
难度:中等 频次:143 题目:给定一个单链表 L 的头节点 head ,单链表 L 表示为: 请将其重新排列后变为: 不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 解题思路:链表找中 ...
最新文章
- C++_泛型编程与标准库(九)——红黑树的使用
- 重定向index.php,使用htaccess将all重定向到index.php
- 数据结构与算法 / 堆结构
- I2C原理及应用实例
- 版本管理工具Git(2)git的使用
- java调用存储过程之环境测试
- 智能集群理论优化控制_数学学科学术报告九:机器人集群的智能协同控制方法_中国计量大学...
- python默认安装位置_如何更改python中pip的默认安装路径
- 系统的性能瓶颈,排查该从哪些方面入手,如何定位?
- 使用visio 2010建立sql server数据模型——手动画、利用逆向工程
- Atitit json序列化工具 JsonParserAtiver 参考 Atitit json序列化原理 序列化是将一个对象变成json格式的字符串,而反序列化是将json格式的字符串变
- python table_python学习之HTML-table标签
- Lomboz介绍+安装方法
- xy苹果助手未受信任_苹果ios企业签名后App无法安装?如何解决?
- LeetCode 1405. 最长快乐字符串**(double,trp)
- grabcut利用matlab如何实现,matlab_grabcut-master 用 实现了 的图像分割的源代码供参考 OpenCV 272万源代码下载- www.pudn.com...
- stata实现经济生态的空间杜宾模型
- Git 各指令的本质,真的是通俗易懂!
- Excel公式大全 excel自动求减 15个常用excel函数公式
- html css燕十八,燕十八_divcss教学笔记.doc
热门文章
- WAV格式中常见的压缩编码
- Java集合(一、HashMap)
- QString 的使用技巧
- 大数据之-Hadoop本地模式_执行Grep官方案例---大数据之hadoop工作笔记0021
- Linux工作笔记035---linux内网测试访问外网网速_外网测试访问内网网速
- android学习笔记---59_各种图形的使用介绍,android炫酷效果的实现
- python 描述符参考文档_python 描述符详解
- 随想录(c语言的优缺点)
- java求最大值时i的值_java 输入一组数组,求最大值。
- eplices如何导入外部代码_#华为云·寻找黑马程序员#【代码重构之路】如何优雅的关闭外部资源...