Leetcode 143. Reorder List(Medium)
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}.
搞了一天多,纠结到一个while 写成了if...........
思路很清晰, 找出链表的中点,翻转第二个链表,合并两个链表。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* findMid(ListNode* head){ // 找出链表的中点,快慢指针解决,很经典。
/*if (head == NULL || head -> next == NULL){
return head;
}*/
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && fast -> next != NULL){ // 注意判断条件很容易出问题
slow = slow -> next;
fast = fast -> next -> next;
}
return slow;
}
ListNode* reverseList(ListNode* head){ // 翻转一个链表
ListNode* newhead = NULL;
while (head != NULL){ // 一开始写成了if 纠结了一天,长记性。。。
ListNode* nextp = head -> next;
head -> next = newhead; newhead = head; // 统一后移
head = nextp;
}
return newhead;
}
ListNode* merge(ListNode* l1, ListNode* l2){ // 合并两个链表
if (l2 == NULL){
return l1;
}
ListNode* head = l1; while (l1 != NULL && l2 != NULL){
ListNode* nextp = l1 -> next;
l1 -> next = l2;
l2 = l2 -> next;
l1 -> next -> next = nextp; l1 = nextp; // l1后移
}
return head;
}
void reorderList(ListNode* head) {
if (head == NULL || head -> next == NULL){
return;
}
ListNode* mid = findMid(head); // 1.找到链表的中点
ListNode* l1 = head;
ListNode* l2 = mid -> next;
mid -> next = NULL; // 2. 断开链表
l2 = reverseList(l2); // 3. 翻转l2
head = merge(l1, l2); // 4. 合并两个链表
}
};
Leetcode 143. Reorder List(Medium)的更多相关文章
- LeetCode: 61. Rotate List(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- 【leetcode】Reorder List (middle)
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 ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
- LeetCode: 54. Spiral Matrix(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
- LeetCode:46. Permutations(Medium)
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...
随机推荐
- Linux如何查看YUM的安装目录
Linux下如何查看使用YUM安装过的包的安装路径呢? 在搞清楚这个问题前,我们先来了解一下YUM. YUM(全称为 Yellow dog Updater, Modified)是一个在Fedora和R ...
- Cas 服务器 Service(Cas客户端)注册信息维护
作为Cas服务器,允许哪些客户端接入与否是通过配置来定义的.对Cas服务器来说,每一个接入的客户端与一个Service配置对应:在Cas服务器启动时加载并注册上这些Service,与之对应的客户端才能 ...
- [20190401]关于semtimedop函数调用.txt
[20190401]关于semtimedop函数调用.txt --//上个星期测试,链接http://blog.itpub.net/267265/viewspace-2639675/--//关于sql ...
- javascript 重要属性之prototype(继承)
转载猫猫小屋 http://www.maomao365.com/?p=831 在javascript中每一个函数都拥有 prototype属性,在javascript中使用prototype,可以向已 ...
- innodb二阶段日志提交机制和组提交解析
前些天在查看关于innodb_flush_log_at_trx_commit的官网解释时产生了一些疑问,关于innodb_flush_log_at_trx_commit参数的详细解释参见官网: htt ...
- Docker 从入门到实践(二)Docker 三个基本概念
一.Docker 的三个进本概念? 了解 Docker 的三个基本概念,就可以大致了解 Docker 的生命周期. 镜像(Image) 容器(Container) 仓库(Repository) 二.镜 ...
- ASP.NET -- WebForm -- .aspx与.aspx.cs文件
ASP.NET -- WebForm -- .aspx与.aspx.cs文件 1. ASP.NET -- WebForm(C#)文件 .aspx文件:是Html页面,页面的布局,样式在该文件中设计. ...
- 【Teradata】日期类型计算
1.EXTRACT(抽取年/月/日/时/分/秒) //抽取年/月/日/时/分/秒 SELECT EXTRACT (YEAR FROM CURRENT_DATE); SELECT EXTRACT (M ...
- SQLite也可能出现死锁
提到锁就不得不说到死锁的问题,而SQLite也可能出现死锁.下面举个例子:连接1:BEGIN (UNLOCKED)连接1:SELECT ... (SHARED)连接1:INSERT ... (RESE ...
- SpringMVC handleMapping映射过程
初始化IOC容器 Spring初始化的时候会优先初始化自定义的类,下面这个就是 org.springframework.web.servlet.mvc.method.annotation.Reques ...