LeetCode Reorder List
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
void reorderList(ListNode* head)
{
if(head == nullptr) return;
int size = 0;
ListNode *ptr = head;
while(ptr != nullptr)
{
size++;
ptr = ptr->next;
}
if(size <= 2) return;
int breakpoint = (size + 1) / 2;
int i = 0;
ptr = head;
while( i++ < breakpoint)
{
ptr = ptr->next;
}
ListNode *ptr2 = head;
while(ptr2!= nullptr && ptr2->next != ptr)
ptr2 = ptr2->next;
if(ptr2 != nullptr)
ptr2->next = nullptr;
ListNode* newheadof2ndPart = reverseLinkedList(ptr);
ptr = head;
for(i = 0; i< breakpoint && ptr != nullptr && newheadof2ndPart != nullptr; i++)
{
ListNode*ptmp = ptr->next;
ListNode*pnextnewhead = newheadof2ndPart->next;
ptr -> next = newheadof2ndPart;
newheadof2ndPart->next = ptmp;
ptr = ptmp;
newheadof2ndPart = pnextnewhead;
}
}
ListNode* reverseLinkedList(ListNode* head)
{
if(head == nullptr) return nullptr;
ListNode* newhead = reverseLinkedList(head->next);
if(head-> next != nullptr)
{
head->next->next = head; //Error, 一定要搞清楚到底是哪个next哦
}
if(newhead == nullptr) newhead = head;
head->next = nullptr;
return newhead;
}
};
LeetCode Reorder List的更多相关文章
- [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 @ Python
原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...
- Leetcode: Reorder List && Summary: Reverse a LinkedList
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 重排链表
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 thi ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- 【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 ...
随机推荐
- <css篇>关于页面的绝对定位与相对定位,且支持页面各元素自适应
(哈哈,第一次写博客,简称处贴?主要记录下平时工作中遇到的问题及其解决方案,方便以后解决此类问题.) 话不多说,上代码看吧 代码有点乱,以后自己能看得懂就行咯 <body > <di ...
- 关于UIView的AutoresizingMask属性的研究
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum ...
- Android 动画分类
一:Tween Animation 动画类型 下面先来看看Android提供的动画类型.Android的animation由四种类型组成 在XML文件中: alpha 渐变透明度动画效果 ...
- select distinct
select distinct select distinct 用于返回表中唯一不同的值. 语法 select distinct 列名称 from 表名称 使用 distinct 关键字 Studen ...
- Python【3】-字典dic和集合set
一.字典dict dict以键值对形式存储,创建方式是用大括号{}并用冒号分隔键和值. >>> d={'chen':60,'zhang':80} >>> print ...
- 运行DbVisualizer报the java_home environment viariable does not point to a working 32-bit JDK OR JRE错误
报这个错误的根本原因就是DbVisualizer和JDK的版本不一致,那么知道原因,修改起来就方便了,要么修改DbVisualizer的版本,要么修改JDK的版本. 1.JDK7 64位 那么就在Db ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- js正则表达式大全(2)
在JAVASCRIPT里面判断一个字符串是否是电子邮件的格式: if(formname.email.value!=formname.email.value.match(/^\w +[@]\w +[.] ...
- [转载] linux查找目录下的所有文件中是否含有某个字符串
链接自 http://blog.sina.com.cn/s/blog_691a84f301015khx.html,并略加修订. 查找目录下的所有文件中是否含有某个字符串 find .|xargs gr ...
- 华为c8812刷机/rom之后,本地音乐无法设置为铃声解决方法【亲测】
手机刷机后,来电铃声和通知铃声可以总能找到办法设置,但闹钟铃声想设置本地音乐,总是没反应,抓狂!!! 后来终于找到了解决办法,很简单:装一个软件——华为音乐,就可以设置了! 吐槽一下,这个手机买电信送 ...