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. maven-web项目中的一些小问题

    1.最新的jetty容器 org.eclipse.jetty 需要JDK1.8的支持. 2.在容器中发布WEB项目时web 中的pom.xml的依赖关系会丧失,依赖和插件需要单独完全编写(尽管IDE会 ...

  2. C#中使用Linq实现全外连接

    每次使用都到处查阅,现在记录下来,备查. var fulljoin = (from s in sampleRegistersjoin t in tensionDatas on new { Beach ...

  3. python3 密码生成器

    用random模块实现按照要求生成一定个数和一定位数的密码: #Author by Andy #_*_ coding:utf-8 _*_ import random checkcode='' code ...

  4. centos下php安装swoole扩展

    官网:http://wiki.swoole.com/wiki/index/prid-1 国内Git镜像:http://git.oschina.net/matyhtf/swoole.git 下载源码后, ...

  5. swift_枚举 | 可为空类型 | 枚举关联值 | 枚举递归 | 树的概念

    ***************可为空的类型 var demo2 :we_demo = nil 上面这个代码串的语法是错的 为什么呢, 在Swift中,所有的类型定义出来的属性的默认值都不可以是nil ...

  6. neo4j中文社区

    关于Neo4j中文社区 官网:http://neo4j.com.cn/ Neo4j 社区为国内具影响力的 Neo4j技术社区,致力于 Neo4j 的技术研究. Neo4j 社区由一批热爱 Neo4j ...

  7. 习题 5: 更多的变量和打印 | 笨办法学 Python

    一. 简述 “格式化字符串(format string)” -  每一次你使用 ' ’ 或 " " 把一些文本引用起来,你就建立了一个字符串. 字符串是程序将信息展示给人的方式. ...

  8. django学习记录--第一个网页“hello django”

    一.安装django 下面两种方法任选其一 1.pip或easy_install 安装 pip install django easy_install django 2.到django官网(https ...

  9. 商业信息管理系统 Bizagi 建模pattern

    WCP 1- Sequence This pattern is used to model dependencies between tasks so that one task cannot sta ...

  10. nodejs解决找不到express命令的问题

    一般的书或者教程上的安装步骤是:(需要是-g,即全局安装) npm install -g express //全局安装 而我们应该多多关注下express的文档,github地址:https://gi ...