Given a singly linked list L: L0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-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}.

class Solution {
public:
void reorderList(ListNode *head) {
if (!head|| !head->next)
return; // partition the list into 2 sublists of equal length
ListNode *slowNode = head, *fastNode = head;
while (fastNode->next) {
fastNode = fastNode->next;
if (fastNode->next) {
fastNode = fastNode->next;
} else {
break;
}
slowNode = slowNode->next;
}
// 2 sublist heads
ListNode *head1 = head, *head2 = slowNode->next;
// detach the two sublists
slowNode->next = NULL; // reverse the second sublist
ListNode *cur = head2, *post = cur->next;
cur->next = NULL;
while (post) {
ListNode* temp = post->next;
post->next = cur;
cur = post;
post = temp;
}
head2 = cur; // the new head of the reversed sublist // merge the 2 sublists as required
ListNode* p = head1, *q = head2;
while (q ) {
ListNode *temp1 = p->next;
ListNode *temp2 = q->next;
p->next = q;
q->next = temp1;
p = temp1;
q = temp2;
}
}
};

143. Reorder List(List)的更多相关文章

  1. leetcode 143. Reorder List 、86. Partition List

    143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...

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

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

  3. 143. Reorder List - LeetCode

    Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...

  4. Java for LeetCode 143 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 ...

  5. 143. 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 thi ...

  6. leetcode 143. 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 do thi ...

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

  8. LeetCode OJ 143. 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 thi ...

  9. [leetcode]143. 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 may not mod ...

  10. 【Leetcode】143. Reorder List

    Question: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You ...

随机推荐

  1. Spring Boot 入门之缓存和 NoSQL 篇(四)

    原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...

  2. Sql Server 2012 存储过程的调试

    [一]Sql Server 关于存储过程调试SQL2000是在查询分析器中的对象浏览器中选中需要调试的存储过程,右键----调试---输入参数开始调试.sqlserver2008中则完全不同,变成了必 ...

  3. opencv读取中文路径报错的问题

    ) ## 经验证,不需要再转bgr,myImread的读图结果已经是和imread一样的 return img

  4. cocos2d-js 3.0 ios平台编译打包

    原帖在http://www.cocoachina.com/bbs/read.php?tid=209356 整理到github的https://github.com/faint2death/cocos2 ...

  5. 关于FPGA复位的认识

    xilinx推荐尽量不复位,利用上电初始化,如果使用过程中需要复位,采用同步高复位. 如果逻辑工程较大,复位扇出会较多,会很影响时序,有以下常用方法: 复位信号按照不同时钟域分为rst0..rstn, ...

  6. linux网络编程、系统编程

    http://blog.csdn.net/lianghe_work/article/category/2871247

  7. GOF23设计模式之组合模式(composite)

    一.组合模式概述 将对象组合成树状结构以表示“部分和整体”层次结构,使得客户可以统一的调用叶子对象和容器对象. (1)组合模式的使用场景   把部分和整体的关系用树形结构来表示,从而使客户端可以使用统 ...

  8. jredis 客户端 使用

    redis学习及实践3---Jedis.JedisPool.Jedis分布式实例介绍 Java中使用Jedis操作Redis Redis客户端:Jedis

  9. 软件官网与memcached介绍

    常见官网 http://www.keepalived.org http://nginx.org/ documentation 模块说明 http://www.apache.org/ https://o ...

  10. Docker - 使用Swarm和compose部署服务(containers)

    前言 在之前使用Docker的过程中,一直是用 Docker run 命令单独启动container后再加入Overlay网络的方式实现部署工作的. 这种方式看似直接,但是随着服务所包含的contai ...