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的更多相关文章

  1. [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 ...

  2. [leetcode]Reorder List @ Python

    原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  8. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  9. 【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 ...

随机推荐

  1. VMWARE里启动kylin16.0时出现'SMBus Host Controller not enabled'(还未进入系统)

    在Vmware里安装完Ubuntu16.10,启动时出现'SMBus Host Controller not enabled'错误提示,进不到图形界面.网上搜了一下,解决办法是在图形界面里进终端窗口, ...

  2. ApplicationContextAware 接口

    一.这个接口有什么用? 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以 ...

  3. web.py框架入门

    在使用微信搭建公众平台的时候,使用的是web.py这个方便简单的框架,学习一下. 框架文档:http://webpy.org/docs/0.3/tutorial.zh-cn  按照文档的内容写一遍程序 ...

  4. HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置。

    检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). - CatcherX 2014-03-11 11:03 27628人阅读 评论(2) 收藏 举报  分类 ...

  5. C# 中正确实现 IDisposable 接口

    作用 此接口的主要用途是释放非托管资源. 当不再使用托管对象时,垃圾回收器会自动释放分配给该对象的内存. 但无法预测进行垃圾回收的时间. 另外,垃圾回收器对窗口句柄或打开的文件和流等非托管资源一无所知 ...

  6. bootstrap中如何让响应式图片(img-responsive)水平居中

    我们在用bootstrap排版内容的时候,有的时候在内容中需要图片水平居中对齐. 一般情况下,我们的图片都使用了 .img-responsive 类来实现响应式图片.如果需要实现响应式图片水平居中,那 ...

  7. PHP global 关键字

    global关键字用于在函数内部访问全局变量. <?php $x = 5; $y = 10; function myTest(){ global $x,$y; $x = $x+$y; } myT ...

  8. 解决VS2015安装Android SDK 后文件不全及更新问题

    近日安装VS2015专业版后.想进行Android开发,就新建了一个Blank app 结果报[值不能为空 null 参数名:path1] 1:首先检查工具 xamarin 工具那设置的SDK路径对不 ...

  9. DotnetBar在VS2010工具箱中不显示问题

    请参考:http://blog.csdn.net/yanbo710148546/article/details/7862819

  10. Ubuntu 16.04 安装 .NET Core[转]

    upir@upir-Rev-1-0:~$ sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos ...