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 ...
随机推荐
- VB检测按键CTRL+C的次数
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As IntegerPriv ...
- java, listmap2json, fastjson
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;impor ...
- 渐变算法的 Java 实现
/** * 指定长度的渐变. * @param c0 起始颜色. * @param c1 结束颜色. * @param len 受限的渐变长度,为保证每一个颜色都不一样,会根据颜色找出长度最大值. * ...
- ActionBar compat 如何禁用ActionBar的显示/隐藏动画
ActionBar compat 如何关闭ActionBar的显示隐藏动画 @Override public boolean onCreateOptionsMenu(Menu menu) { //消除 ...
- python 汇总
TypeError: ReadExcelList() takes exactly 1 argument (2 given) 传入的参数有问题
- CSS3支持box-flex弹性布局
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- Spring 相关jar包详细介绍
文章转自:http://blog.csdn.net/farawayhome/article/details/6623946 aspectj目录下是在Spring框架下使用aspectj的源代码和测试程 ...
- 原生js通过prottype写的一个简单拖拽
<!DOCTYPE html> <head> <meta charset="utf-8"/> <title></title&g ...
- sass中中文注释报错
最近项目中用到了sass来编译css,但是scss代码中写了中文注释后编译报错, 经过查找文档和资料,终于找到了解决办法,即在scss文件顶部加上@charset "utf-8"; ...
- Linux x64 下 Matlab R2013a 300 kb 脚本文件调试的 CPU 占用过高问题的解决办法
(1) 系统+软件版本 CentOS 6.5 (Final), 64 位,内核initramfs-2.6.32-431.5.1.el6.x86_64, MATLAB Version: 8.1.0.60 ...