Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→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...........

思路很清晰,  找出链表的中点,翻转第二个链表,合并两个链表。

b 站视频讲解

/**
* 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)的更多相关文章

  1. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  2. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  3. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

  4. 【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 ...

  5. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  6. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  7. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  8. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

  9. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

随机推荐

  1. Java中线程的同步问题

    在生活中我们时常会遇到同步的问题,而且大多数的实际问题都是线程的同步问题 我这里以生活中的火车售票来进行举例: 假设现在我们总共有1000张票要进行出售,共有10个出售点,那么当售票到最后只有一张票时 ...

  2. ASP.NET MVC之从控制器传递数据到视图方式

    为了演示,先定义一个类 新建基本项目在Models文件夹下定义如下类: public class Person { public int Id { get; set; } public string ...

  3. 修改主机时间对MySQL影响

    背景 在装机实施时,BIOS忘记调整时间,导致服务器时间与CST不符合:待发现问题时,MySQL环境已经在运行,所以只能通过操作系统进行更改:但是更改完成后,MySQL进行重启时发生了问题.以下为问题 ...

  4. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

  5. February 21st, 2018 Week 8th Wednesday

    Our life is what our thoughts make it. 我们的思想成就了我们的生活. The mind is everything. What you think, you be ...

  6. Python闭包和装饰器再复习

    闭包 闭包的定义 在一个外函数中定义了一个内函数,并且内函数用到了外部函数的变量,而且外函数的返回值是内函数的引用,这就构成了一个闭包. 一般情况下,在我们认知当中,如果一个函数结束,函数的内部所有东 ...

  7. Git版本管理工具常用命令说明

    Git常用命令 $ touch README.md 创建一个README.md文件 $ git init  创建本地仓库(repository),将会在文件夹下创建一个 .git 文件夹,.git 文 ...

  8. Django forms 关于select和checkbox设置初始选中值

    Django的forms和models一样很牛逼.他有两种功能,一是生成form表单,还有就是form表单的验证. 这里主要说一下生成form表单时经常用到的需要设置 初始值 / 默认值 的情况. 1 ...

  9. 【车】汽车X40保养

    参考文档: [养车成本]小保养331元,奔腾X40养车成本调查

  10. 将逗号分隔的字符串转换为Python中的列表

    给定一个字符串: 它是由逗号分隔的几个值的序列: mStr = '192.168.1.1,192.168.1.2,192.168.1.3' 如何将字符串转换为列表? mStr = ['192.168. ...