Leetcode ReorderList
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}
.
(1)找出中间节点
(2)将后部分节点反转
(3)将前部分节点和后部分节点合并
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <list> using namespace std; struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
}; void printList(ListNode* head){
while(head!=NULL){
cout<<"->"<<head->val;
head = head->next;
}
cout<<endl;
} ListNode* reverse(ListNode *head){
ListNode *pre = head;
ListNode *p = head->next;
head->next =NULL;
while(p!=NULL){
ListNode* tmp = p->next;
p->next = pre;
pre = p;
p = tmp;
}
return pre;
} ListNode* merge(ListNode *head1, ListNode *head2){
ListNode* p = head1;
while(head2!=NULL){
ListNode *tmp = head2->next;
head2->next = p->next;
p->next = head2;
p = p->next->next;
head2 = tmp;
}
return head1; } void reorderList(ListNode *head){
if(head == NULL || head->next == NULL) return;
ListNode *slow = head,*fast = head;
while(fast->next && fast->next->next){
slow = slow->next;
fast = fast->next->next;
}
ListNode *head2 = slow->next;
slow->next = NULL;
ListNode *head1 = head;
//printList(head2);
head2 = reverse(head2);
//printList(head2);
head = merge(head1,head2);
} int main(){
ListNode *head= new ListNode();
ListNode *p = head;
for(int i =; i <= ; ++ i){
ListNode *a= new ListNode(i);
p->next = a;
p = a;
}
reorderList(head);
printList(head);
}
Leetcode ReorderList的更多相关文章
- leetcode — reorder-list
/** * Source : https://oj.leetcode.com/problems/reorder-list/ * * Given a singly linked list L: L0→L ...
- reorder-list leetcode C++
Given a singly linked list L: L 0→L 1→-→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→- You ...
- [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算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LEETCODE OJ】Reorder List
Problem link: http://oj.leetcode.com/problems/reorder-list/ I think this problem should be a difficu ...
- 【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 ...
随机推荐
- jquery学习笔记---闭包,原型链,this关键字
网上的资料很多,关于闭包,原型链,面向对象之内的.本人也有一点自己的总结. 关于this: this 的值取决于 function 被调用的方式,一共有四种, 如果一个 function 是一个对象的 ...
- JavaScript Canvas 根据像素点取位置
<html> <body> <canvas id="canvas" width="100" height="100&qu ...
- Pyqt Smtplib实现Qthread多线程发送邮件
一. smtplib 的介绍 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]]) SMTP类构造函数,表示与SMTP服务器之间的连接 ...
- 几年前做家教写的C教程(之二)
C语言学习宝典(2) 认识C语言中的运算符: (1)算术运算符 (+ - * / %) (2)关系运算符 (> < == >= <= != ) (3 ...
- 【JAVA多线程安全问题解析】
一.问题的提出 以买票系统为例: class Ticket implements Runnable { public int sum=10; public void run() { while(tru ...
- spring 集成 log4j 配置
在web.xml中增加如下代码: <context-param> <param-name>log4jConfigLocation</param-name> < ...
- mac liteIDE调试配置
http://studygolang.com/articles/1636 brew install https://raw.github.com/Homebrew/homebrew-dupes/mas ...
- 2-05使用SQL语句创建数据库2
使用SQL语句创建多个数据文件和日志文件: USE master--指向当前使用的数据库 GO--批处理的标志 CREATE DATABASE E_Market--创建E_market数据库 ON P ...
- CQRS及.NET中的参考资料
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:CQRS作为一种设计模式,其实一点都不新鲜了.不过今天有朋友感叹.NET朋友也关注CQ ...
- Linux编程(3) MakeFile
1. 在Linux中,make工具可以维护程序模块关系和生成可执行程序.它可根据程序模块的修改情况重新编译链接生成中间代码或最终的可执行程序.执行make命令,需要一个名为Makefile的文本文件, ...