FROM

思路

1. 将后半段截取下来再倒序, 插入到前半段, 时间复杂度为 o(n)

代码

#include <iostream>
using namespace std;struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {
public:void reorderList(ListNode *head) {if(head == NULL)return;int size = 0;ListNode *curNode = head;while(curNode) {size++;curNode = curNode->next;}    int step = size - (size >> 1); // pick the larger partListNode *cursor1 = head, *cursor2 = head;while(--step)cursor2 = cursor2->next;ListNode *tmp = cursor2;if(tmp != NULL) {cursor2 = tmp->next;tmp->next = NULL;}cursor2 = reverseList(cursor2);while(cursor2) {ListNode *tmp = cursor2;cursor2 = cursor2->next;tmp->next = cursor1->next;cursor1->next = tmp;cursor1 = tmp->next;}}ListNode* reverseList(ListNode* head) {if(head == NULL)return head;ListNode *curNode = head;while(curNode->next) {ListNode *nextNode = curNode->next;curNode->next = nextNode->next;nextNode->next = head;head = nextNode;}return head;}
};int main() {int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8};ListNode* list[10];int len = 3;for(int i = 0; i < len; i ++) list[i] = new ListNode(arr[i]);for(int i = 0; i < len-1; i ++)list[i]->next = list[i+1];(new Solution())->reorderList(list[0]);ListNode *lists = list[0];while(lists) {cout << lists->val << " ";lists = lists->next;}cout << endl;return 0;
}

  

Leetcode: Reorder List相关推荐

  1. LeetCode - Reorder List

    Reorder List 2014.1.13 22:07 Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→ ...

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

  3. LeetCode Reorder List

    题意:给出一个链表,重新排列链表,形式为第一个-->倒数第一个->第二个->倒数第二个 思路:将数组分成两半,然后将后半部反转,再拼接 代码发下: class Solution {p ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. [LeetCode] 143. Reorder List_Middle tag: Linked 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 mo ...

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

  7. 【重点】LeetCode 143. Reorder List

    LeetCode 143. Reorder List Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4254860.html 这段代码有值得学习 ...

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

  9. LeetCode:937. Reorder Log Files

    051401 题目 You have an array of logs. Each log is a space delimited string of words. For each log, th ...

最新文章

  1. R语言Box-Cox变换实战(Box-Cox Transformation):将非正态分布数据转换为正态分布数据、计算最佳λ、变换后构建模型
  2. C#读取数据库图片显示、缩小、更新
  3. Mac OS X 中的脚本语言应用
  4. 马哥linux2020博客,马哥linux学习(bash shell学习)
  5. 装配图位置偏转怎么调整_物理微课|匀变速直线运动、电容器动态分析及磁偏转技巧、方法、模型...
  6. 【LeetCode - 32】最长有效括号
  7. SPI子系统分析之一:框架
  8. mySQL及可视化界面navicat在window的配置
  9. 显示器尺寸对照表_电脑液晶屏尺寸如何计算,液晶屏尺寸对照表
  10. angular封装七牛云图片上传,解决同一页面多个上传按钮分别上传
  11. matlab推挽用的变压器,推挽逆变+全桥整流~~~~逆变器变压器设计
  12. cs显示服务器连接失败是什么原因,CS1.5服务器连接常见问题解决 解决常见Windows 7无法识别网络的问题...
  13. Johnson–Lindenstrauss Lemma
  14. 里氏替换原则——举例说明Java设计模式中的里氏替换原则
  15. 计算机教学中心理反思,多媒体教学反思
  16. win10家庭版安装Hyper-v
  17. 体育教学与计算机技术的结合点,【大学教育论文】虚拟现实技术在高校体育教育的应用(共2757字)...
  18. 白魔法师(牛客小白月赛25 图、并查集)
  19. 【重新定义matlab强大系列八】利用matlab求局部值(函数islocalmax求局部最大值+函数islocalmin求局部最小值)
  20. 【元胞自动机】元胞自动机单车道交通流(时空图)【含Matlab源码 1681期】

热门文章

  1. Apache Spark 2.0预览: 机器学习模型持久化
  2. 用PHP开发命令行工具
  3. Tungsten Fabric SDN — VNC API — API Client 的 Python SDK
  4. 5G NR — 频率、频段、载波、载频、载波带宽
  5. 拒绝从入门到放弃_《Python 核心编程 (第二版)》必读目录
  6. NR 1G - 5G基站介绍
  7. Struts2如何实现MVC,与Spring MVC有什么不同?
  8. 各种编程语言功能综合简要介绍(C,C++,JAVA,PHP,PYTHON,易语言)
  9. 我的Android进阶之旅------gt;怎样在多个LinearLayout中加入分隔线
  10. LeetCode:326. Power of Three