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}.

快慢指针找到链表中点,将链表分为两段,翻转后半段,再合并两个子链表。

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode *head) {
if (head == nullptr || head->next == nullptr || head->next->next == nullptr) {
return;
}
ListNode *fast = head, *slow = head;
while (fast->next && fast->next->next) {
fast = fast->next->next;
slow = slow->next;
}
fast = slow->next;
slow->next = nullptr;
fast = reverseList(fast);
head = mergeList(head, fast);
}
private:
ListNode * reverseList(ListNode *head) {
ListNode dummy(-);
ListNode *p = &dummy;
while (head) {
ListNode *temp = head->next;
head->next = p->next;
p->next = head;
head = temp;
}
return dummy.next;
} ListNode *mergeList(ListNode *l1, ListNode *l2) {
ListNode dummy(-);
ListNode *p = &dummy;
while (l1) {
p->next = l1;
l1 = l1->next;
p = p->next;
if (l2) {
p->next = l2;
l2 = l2->next;
p = p->next;
}
}
return dummy.next;
}
};

【Leetcode】Reorder List的更多相关文章

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

  2. 【Leetcode】Reorder List JAVA

    一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must ...

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

  4. 【LeetCode】Reorder Log Files(重新排列日志文件)

    这道题是LeetCode里的第937道题. 题目描述: 你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写 ...

  5. 【leetcode】Reorder List (python)

    问题的思路是这样: 循环取头部合并,事实上也能够换个角度来看,就是将后面的链表结点,一次隔空插入到第一部分的链表中. class Solution: # @param head, a ListNode ...

  6. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. STM32 FATFS文件系统移植

    http://www.360doc.com/content/11/1221/10/7736891_173820469.shtml

  2. c#反射优化 表达式树

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  3. SQL获取本周,上周,本月,上月第一天和最后一天[注:本周从周一到周天]

    DECLARE @ThisWeekStartTime NVARCHAR(100),@ThisWeekEndTime NVARCHAR(100),--本周 @LastWeekStartTime NVAR ...

  4. 设计模式3---工厂模式(Factory Pattern简单工厂、工厂方法、抽象工厂)

    工厂模式:主要用来实例化有共同接口的类,工厂模式可以动态决定应该实例化那一个类.工厂模式的形态工厂模式主要用一下几种形态:1:简单工厂(Simple Factory).2:工厂方法(Factory M ...

  5. APUE(5)---标准I/O库 (2)

    六.读和写流 一旦打开了流,则可在3种不同类型的非格式化I/O中进行选择,对其进行读.写操作:1)每次一个字符的I/O,一次读或写一个字符,如果刘时代缓冲的,则标准I/O函数处理所有缓冲:2)每次一行 ...

  6. python字符串字典列表互转

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  7. Mysql主主复制+keepalived

    1>环境 Master1 10.0.0.201 Master2 10.0.0.202 2>Master1操作授权 mysql>grant replication slave on * ...

  8. C/C++学习的50个经典网站

    C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些网站提供c/c++源代码.这份清单提供了源代码的链接以及它们的小说明.我已尽力包括最佳的C/C++源代码的网站.这不是一个完整的清 ...

  9. Replication--镜像+复制

    场景:主服务器:Server1从服务器:Server2订阅服务器: Server3镜像DB: RepDB配置:1>配置SERVER3为分发服务器,在Server3上指定发布服务器SERVER1和 ...

  10. Nginx源码编译

    1. 概述 有时由于添加了自己编写的c++模块,或者改了源码的一些名称.配置什么的,需要自行编译nginx. 可以先下下来源码,然后需要的话就自己改下源码或增加模块,最后再编译成linux或者wind ...