用快慢双指针,可以使慢指针到达中间的时候快指针到达最后一个元素(奇数),或者倒数第二个元素(偶数)。慢指针后面的元素是后半个链表,把后半个链表进行reverse,然后再插在原来的链表中就可以了

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
ListNode* reverse(ListNode* head)
{
ListNode* pre;
ListNode* cur;
cur = head;
pre = NULL;
while (cur!= NULL)
{
ListNode* next;
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
public:
void reorderList(ListNode* head) {
if (head==NULL||head->next == NULL || head->next->next == NULL)
return;
ListNode* fast;
ListNode* slow;
fast = head;
slow = head;
while (fast->next&&fast->next->next)//出来之后slow的下一个就是后半段.
{
fast = fast->next->next;
slow = slow->next;
}
ListNode*q;
q = reverse(slow->next);
slow->next = NULL;
ListNode*p;
p = head;
while (p&&q)
{
ListNode* temp;
temp = new ListNode(q->val);
temp->next=p->next;
p->next = temp;
p = temp->next;
q = q->next;
}
return;
}
};

  

leetcode143. Reorder List的更多相关文章

  1. Leetcode143. Reorder List重排链表

    给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例 1: ...

  2. [Swift]LeetCode143. 重排链表 | 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 ...

  3. LeetCode143: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 d ...

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

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

  6. 13. Reorder List

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

  7. String reorder

    本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions) Description For th ...

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

  9. 62. 链表重排[Reorder List]

    [本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...

随机推荐

  1. sonar-代码扫描

    代码测试sonar 1.下载地址: https://www.sonarqube.org/downloads/ 2.配置jdk环境(版本要求1.8+) [root@localhost#>> ...

  2. 005-Spring Boot配置分析-配置文件application、EnvironmentPostProcessor、Profiles

    一.配置文件application 默认配置文件application.propertie或者application.yml,可同时存在 application.propertie增加配置:local ...

  3. ABP给WebApi添加性能分析组件Miniprofiler

    在ABP的WebApi中,对其性能进行分析监测是很有必要的.而悲剧的是,MVC项目中可以使用的MiniProfiler或Glimpse等,这些都不支持WebApi项目,而且WebApi项目通常也没有界 ...

  4. Spark SQL大数据处理并写入Elasticsearch

    SparkSQL(Spark用于处理结构化数据的模块) 通过SparkSQL导入的数据可以来自MySQL数据库.Json数据.Csv数据等,通过load这些数据可以对其做一系列计算 下面通过程序代码来 ...

  5. Java菜鸟浅谈OCR

    什么是OCR? 粗暴点说就是图片文本识别!正规点的说法就是:(Optical Character Recognition,光学电子识别) 最近公司开展新项目,考虑到实名认证这方面,然后还要上传身份证正 ...

  6. Android应用程序MVC框架实例分析

    问题提出:如何优雅地分离出应用程序的状态.用户交互和数据表现?如何通过框架体现工程的高性能.高灵活性.高响应性? MVC定义:model.view.controller三者的有机组合,分别表示:模型. ...

  7. [HDU4864]Task (贪心)

    此图和上一篇博客的图一起看有奇效 题意 https://vjudge.net/problem/HDU-4864 思路 贪心 代码 by lyd 我实在是敲不来 #include <iostrea ...

  8. __x__(42)0910第六天__表格布局 老旧的布局方法

    table 布局 不易于维护,耦合太严重了. 不利于搜索引擎检索. 效果图: html代码: <!doctype html> <html> <head> <m ...

  9. Android 小票打印USB

    第一步USB通信: Usb设备有两种,Host与Accessory 简单来说是主模式与从模式,主模式则android设备给外设供电,反之,外设给android设备充电,对于小票打印,使用的是Host模 ...

  10. TextInputLayout 用法

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...