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. MapReduce ----倒排索引

    分别建立三个文件: file1txt file2.txt file3.txt 文件内容分别是: MapReduce is simple 和 MapReduce is powerful is simpl ...

  2. 洗礼灵魂,修炼python(84)-- 知识拾遗篇 —— 网络编程之socket

    学习本篇文章的前提,你需要了解网络技术基础,请参阅我的另一个分类的博文:网络互联技术(4)——计算机网络常识.原理剖析 网络通信要素 1.IP地址: 用来标识网络上一台独立的终端(PC或者主机) ip ...

  3. SQL Server has encountered 1 occurrence(s) of cachestore flush for the 'Object Plans' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.

    2017-11-01 09:49:44.35 spid166 SQL Server has encountered 1 occurrence(s) of cachestore flush for th ...

  4. c/c++ 友元的简单应用

    友元的简单应用 1,对象 + 对象,或者,对象 + 数字,可以用类的成员函数去重载+号函数,但是,数字 + 对象就不能用类的成员函数去重载+号函数了, 因为编译器会把数字 + 对象翻译成数字.oper ...

  5. Flask中的CBV

    Flask中的CBV 第一种 class Index(views.MethodView): methods = ['GET', 'POST'] decorators = [] def get(self ...

  6. IEEE754浮点数的表示方法

    https://blog.csdn.net/K346K346/article/details/50487127

  7. docker往阿里云推镜像和打包镜像

    向仓库推镜像 1. 登录到阿里云docker镜像站点,然后创建仓库. 2.要按照阿里云官方给定的仓库名称来使用,所以我们一般都要继续给准备要上传的镜像二次添加标签,如下所示: 3.在终端登录阿里云站点 ...

  8. AppiumLibrary常用关键字

    通过上一章节,open application关键字的使用,相信大家对手机自动化充满了兴趣,那么今天这一章节,主要介绍AppiumLibrary中常用关键字的使用. 一.实用函数 关键字 含义 实例 ...

  9. 平方根的C语言实现(一) —— 浮点数的存储

    版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/7203254.html 作者:窗户 Q ...

  10. 设计模式のCompositePattern(组合模式)----结构模式

    一.产生背景 又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象.组合模式依据树形结构来组合对象,用来表示部分以及整体层次.这种类型的设计模式属于结构型模式,它创建了对象组的树形结构. 这种模 ...