[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 this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}
, reorder it to {1,4,2,3}
.
OH! MY GOD! I HATE LINKED LIST!
看似简单,实现起来总会遇到各种指针错误,写程序之前最好先在纸上好好画画,把各种指针关系搞搞清楚。
本题的想法就是先将列表平分成两份,后一份逆序,然后再将两段拼接在一起,逆序可用头插法实现。注意这里的函数参数要用引用,否则无法修改指针本身的值!
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 void reverseList(ListNode *&head) { 12 ListNode *h = new ListNode(0); 13 ListNode *tmp; 14 while (head != NULL) { 15 tmp = head->next; 16 head->next = h->next; 17 h->next = head; 18 head = tmp; 19 } 20 head = h->next; 21 } 22 23 void twistList(ListNode *&l1, ListNode *&l2) { 24 ListNode *p1, *p2, *tmp; 25 p1 = l1; p2 = l2; 26 while (p1 != NULL && p2 != NULL) { 27 tmp = p2->next; 28 p2->next = p1->next; 29 p1->next = p2; 30 p1 = p1->next->next; 31 p2 = tmp; 32 } 33 } 34 35 void reorderList(ListNode *head) { 36 if (head == NULL || head->next == NULL || head->next->next == NULL) { 37 return; 38 } 39 ListNode *slow, *fast; 40 slow = head; fast = head; 41 while (fast != NULL && fast->next != NULL) { 42 slow = slow->next; 43 if (fast->next->next == NULL) { 44 break; 45 } 46 fast = fast->next->next; 47 } 48 ListNode *l2 = slow->next; 49 slow->next = NULL; 50 reverseList(l2); 51 twistList(head, l2); 52 } 53 };
转载于:https://www.cnblogs.com/easonliu/p/3642822.html
[Leetcode] Reorder List相关推荐
- 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→ ...
- Leetcode: Reorder List
FROM 思路 1. 将后半段截取下来再倒序, 插入到前半段, 时间复杂度为 o(n) 代码 #include <iostream> using namespace std;struct ...
- LeetCode Reorder List
题意:给出一个链表,重新排列链表,形式为第一个-->倒数第一个->第二个->倒数第二个 思路:将数组分成两半,然后将后半部反转,再拼接 代码发下: class Solution {p ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- [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 ...
- 【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 ...
- 【重点】LeetCode 143. Reorder List
LeetCode 143. Reorder List Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4254860.html 这段代码有值得学习 ...
- 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 ...
- 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 ...
最新文章
- 第二章 数据的表示和运算 2.1.3 字符与字符串 [计算机组成原理笔记]
- 语言中要输出表格_C语言 | 表格输出若干人的信息
- TimeSpan 用法 求离最近发表时间的函数
- 老项目引入masonry后报错unrecognized selector sent to instance
- [C#参考]字符编码
- 【连载】如何掌握openGauss数据库核心技术?秘诀四:拿捏事务机制(4)
- Android软件开发-ProgressBar
- 简单的php文件上传实例
- java stream findany_Java Stream findFirst() vs findAny() API With Example
- Android UUID.randomUUID()生成唯一数,1到100随机数
- 开发者从应用程序商店难以赚到真金白银
- c语言画bode图程序,根据上位机测得的Bode图的幅频特性,就能确定系统(或环节)的相频特性,试问这在什么系统时才能实现?...
- Java网络编程——UDP编程
- 黑鲨重装计算机安装无法继续,黑鲨装机,小编教你黑鲨怎么安装win7
- excel的sumif()函数和sumifs()函数
- 整理:不用ACE你不知道ACE有多烂,给饱受ACE折磨的弟兄们散分了。
- 如何让自己的博客主动被搜索引擎收录
- 微信公众号--php
- 《SoloBug - bug管理系统》-Echarts+Ajax实现图表数据异步加载
- c语言e怎么表示_如何一个月学完c语言
热门文章
- X264_最简单的视频编码实现(YUV420编码H264)
- java命令_Java程序员,不得不会的JDK jstack命令工具
- c 字符串数组_C语言探索之旅 | 第二部分第四课:字符串
- asp.net Ajax表单提交 二种方式数据处理 asp.net
- mysql strchr_PHP字符串函数之 strstr stristr
- ORA-28000 the account is locked的解决办法
- 【PAT乙】1047 编程团体赛 (20分)字符串,桶排序
- 【HDOJ2087】剪花布条(KMP)
- 【codevs2627】村村通
- 服务器不知道循环生成文件,Windows服务器下PowerShell命令往服务器共享文件夹进行文件拷贝、循环文件重命名...