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. 根据标签中动态获取的值绑定特定的class

    数据有mock数据获取 mock文件: index文件: 引入文件index: 获取数据函数: 根据获取的标签内容给定不同的样式:

  2. java中利用dom4j解析XML文件

    官网下载Dom4j地址:https://dom4j.github.io/ 注意:使用Dom4j开发,需下载dom4j相应的jar文件 题目:后台利用dom4j解析student.xml文件,并返回Li ...

  3. C# -- 二分法查找

    二分法查找:适用于已经排序好的数组 1.二分法查找(入门案例) static void Main(string[] args) { , , , , , , , , , , , , , , , , , ...

  4. js原生动态创建表格

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. mysql数据库显示 1164 table *** doesn't exist

    问题出现场景: 以前mysql安装在C盘,后来重装系统,将mysql安装在了D盘,重装之前,将mysql的Data 文件夹备份了下来,mysql重新安装好之后,将原来的Data 文件夹内的数据库文件夹 ...

  6. vue过渡动画

    概述 Vue 在插入.更新或者移除 DOM 时,提供多种不同方式的应用过渡效果.包括以下工具: 在 CSS 过渡和动画中自动应用 class 可以配合使用第三方 CSS 动画库,如 Animate.c ...

  7. centos7下升级SSH

    Linux 发行版中集成的 SSH 软件版本都比较老,存在着一些漏洞和安全隐患,需要升级 SSH 服务软件修补漏洞提升系统安全. SSH:SSH 有许多标准,通常 Linux 中 ( Redhat, ...

  8. centos7下安装docker(18.1docker日志---logging driver)

    将容器的日志发送到STDOUT和STDERR是docker的默认日志行为.实际上,docker提供了多种日志机制帮助用户从容器中提取日志,这些机制被称为logging driver docker的默认 ...

  9. day2 编程语言介绍、Python运行程序的两种方式、变量

    一 编程语言介绍 1. 机器语言 用计算机能理解的二进制指令直接编写程序,直接控制硬件 2. 汇编语言 用英文标签取代二进制指令编写程序,本质也是直接控制硬件 3. 高级语言 用人能理解的表达方式去编 ...

  10. 01 python初学(注释、交互、if while for)

    为了能生存下去,一定要坚持学习! 目录  1. 注释 2. 用户交互 3. if .while.for 语句 1. 注释  单行注释: # 多行注释: 三个单引号 || 三个双引号 2. 用户交互: ...