[Linked 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→…
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}
.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head)
{
ListNode* cur = head;
ListNode* newHead = NULL;
ListNode* next = NULL;
while(cur){
next = cur->next;
cur->next = newHead;
newHead = cur;
cur = next;
}
return newHead;
} void reorderList(ListNode* head)
{
if(head==NULL || head->next==NULL){
return;
}
ListNode* slow = head;
ListNode* fast = head;
ListNode* pre = NULL;
while(fast){
pre = slow;
slow = slow->next;
fast->next ? fast = fast->next->next : fast = NULL;
}
pre->next = NULL;
ListNode* head2 = reverseList(slow); ListNode* cur = head;
ListNode* cur_next = NULL;
ListNode* cur2 = head2;
ListNode* cur2_next = NULL;
while(cur2){
cur_next = cur->next;
cur2_next = cur2->next; cur->next = cur2;
cur2->next = cur_next; cur = cur_next;
cur2 = cur2_next;
}
}
};
[Linked List]Reorder List的更多相关文章
- [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] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [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】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 ...
- 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 ...
- 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 ...
- 62. 链表重排[Reorder List]
[本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...
- 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 ...
- Reorder List [LeetCode]
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 ...
随机推荐
- vs2010中出现:程序管理器匹配不正确错误
http://bbs.csdn.net/topics/360074795 这是一篇csdn上的讨论帖,第15楼给出了正解
- c# 配置连接 mysql
今天复习了下c#连接mysql 记录下来方便自己也方便别人! 使用vs2010连接mysql 数据库, 1.装连接驱动,使用Connector/Net 连接驱动!下载地址:http://dev.my ...
- 浅谈window.attachEvent
以前写 JavaScript 脚本时,事件都是采用object.event = handler;的方式初始化.这种方式对于 Internet Explorer.Mozilla/Firefox 和 Op ...
- linux下tar用法
以下是linux下tar的用法,转一下,以便方便自己看(这里没把rar,zip类的转过来,一般rar,zip在linux下基本没人用,基本上是zip,unzip,rar,unrar,这些命令,并且ra ...
- HTML实体符号
http://www.php100.com/html/program/html/2013/0903/1052.html
- BZOJ 2440 完全平方数(莫比乌斯反演,容斥原理)
http://www.lydsy.com/JudgeOnline/problem.php?id=2440 题意:求第K个没有平方因子的数 思路:首先,可以二分数字,然后问题就转变成x以内有多少无平方因 ...
- T-SQL 基于列的逻辑表达式 (CASE)
CASE简介 基于列的逻辑表达式,其实就是CASE表达式.可以用在SELECT,UPDATE,DELETE,SET以及IN,WHERE,ORDER BY和HAVING子句之后.由于这里讲的是T-SQL ...
- expect set timeout -1 永不超时
. ~/.bash_profile passwd='xxx' expect <<! set timeout -1 spawn rsync -avH /webapps/Seeyon/A8/b ...
- Linux系统编程(18)——正则表达式实用举例
匹配特定字符串: 只能输入长度为3的字符:"^.{3}$". 只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$". 只能输入由26个大写英文字母组 ...
- C语言的本质(34)——静态库
库是一种软件组件技术,库里面封装了数据和函数. 库的使用可以使程序模块化. Windows系统包括静态链接库(.lib文件)和动态链接库(.dll文件). Linux通常把库文件存放在/usr/lib ...