[Leetcode Week6]Reorder List
Reorder List 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/reorder-list/description/
Description
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}.
Solution
typedef struct ListNode ListNode;
// reverse linked list with head node
void reverseList(struct ListNode* head) {
if (head == NULL || head -> next == NULL || head -> next -> next == NULL)
return;
ListNode *preNode = head -> next;
ListNode *tail = preNode;
ListNode *curNode = preNode -> next;
ListNode *nextNode;
while (curNode != NULL) {
nextNode = curNode -> next;
curNode -> next = preNode;
preNode = curNode;
curNode = nextNode;
}
head -> next = preNode;
tail -> next = NULL;
}
void reorderList(struct ListNode* head) {
if (head == NULL || head -> next == NULL || head -> next -> next == NULL)
return;
ListNode *mid = head, *tail = head -> next;
while (tail && tail -> next) {
mid = mid -> next;
tail = tail -> next -> next;
}
reverseList(mid);
ListNode *qNode = mid -> next, *qNextNode;
mid -> next = NULL;
ListNode *preNode = head;
ListNode *curNode = preNode -> next;
while (curNode != NULL) {
preNode -> next = qNode;
qNextNode = qNode -> next;
qNode -> next = curNode;
preNode = curNode;
curNode = curNode -> next;
qNode = qNextNode;
}
preNode -> next = qNode;
}
解题描述
这道题刚拿上手还是有点不知所措。想得清楚怎么画图,就是不知道怎么实现。后面才慢慢想出,在原来的链表中间的地方截断成2个链表,将后半截用带头节点链表反转的方式进行反转,之后再合并这两个量表。主要还是考验了链表的操作,包括:
- 快速找到链表中间节点
- 反转链表
- 链表合并
[Leetcode Week6]Reorder List的更多相关文章
- 【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 ...
- [LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序
You have an array of `logs`. Each log is a space delimited string of words. For each log, the first ...
- 【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 ...
- 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 ...
- 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 ...
- [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...
- 【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 Solutions : Reorder List
→-→Ln-1→Ln, reorder it to: L→Ln-2→- You must do this in-place without altering the nodes' values. Fo ...
- 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 ...
随机推荐
- 怎样安装PyCharm
在地址栏输入http://www.jetbrains.com/pycharm/ 打开PyCharm官网 http://idea.lanyus.com/
- 第八篇Python基本数据类型之列表、元组与字典
列表 写在最前,必须要会的:append(),extend(),insert(),索引,切片,循环 list 是一个类,是个对象 列表用 方括号[]括起来的,[]内以逗号分割每个元素,列表中的元素可 ...
- 【java并发编程实战】第六章:线程池
1.线程池 众所周知创建大量线程时代价是非常大的: - 线程的生命周期开销非常大:创建需要时间,导致延迟处理请求,jvm需要分配空间. - 资源消耗:线程需要占用空间,如果线程数大于可用的处理器数量, ...
- Visual Studio 2014安装包
点击下载
- BZOJ 4184 shallot 线性基+分治
Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从小葱手里拿走一颗小葱苗,并且让小葱从 ...
- HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)
Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...
- 程序运行bug查看
1.左击计算机进入管理,点击windows日志,查看程序信息. 可以方便看到报错信息.
- 在使用easyUI时,js,css样式都加载了 但是图标加载不了
可能的问题:web.xml 配置了这些 <servlet-mapping> <servlet-name>default</servlet-name> <url ...
- vs2017切换设计、拆分假死的解决
今天安装了vs2017版把vs2015卸载了,原因就是vs2015运行webform后ajaxpro总是会出现time out现象.太难受了,所以决定体验一下新版本. 安装了vs2017后ajaxpr ...
- Javascript中闭包常用场景
一.前言 有准备去看Javascript框架,所以对于Javascript和原型继承有必要去了解,这边小记一下闭包比较好的例子~ 二.正文 先来一下“闭包”的解释~ 一个拥有许多变量和绑定了这些变量的 ...