Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

You may not modify the values in the list’s nodes, only nodes itself may be changed.
You must do this in-place without altering the nodes’ values.

Example 1:

Given 1->2->3->4, reorder it to 1->4->2->3.

Example 2:

Given 1->2->3->4->5, reorder it to 1->5->2->4->3.

Solution:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {public:void reorderList(ListNode* head) {if(head == nullptr)return;ListNode* mid = head;ListNode* p = head;while(p && p->next){mid = mid->next;p = p->next->next;}if(mid->next){ListNode* pnew = reverse(mid->next); // 逆转后半部分mid->next = nullptr; // 截断两个链表p = head;while(p && pnew)  // 将逆转后的链表插入到前半部分链表{ListNode* tmp = p->next;p->next = pnew;pnew = pnew->next;p->next->next = tmp;p = p->next->next;}}}private:ListNode* reverse(ListNode* head){ListNode dummy{-1, head};ListNode* p = &dummy;ListNode* prev = p->next;ListNode* cur = prev->next;while(prev && cur){prev->next = cur->next;cur->next = p->next;p->next = cur;cur = prev->next;}return dummy.next;}
};

Leetcode - 143. Reorder List相关推荐

  1. 【重点】LeetCode 143. Reorder List

    LeetCode 143. Reorder List Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4254860.html 这段代码有值得学习 ...

  2. [LeetCode] 143. Reorder List_Middle tag: Linked List

    Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You may not mo ...

  3. [leetcode]143. 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 ...

  4. LeetCode 143. Reorder List

    Tag:List Difficulty:Medium Problem 重排链表  给定一个单链表 L 的头节点 head ,单链表 L 表示为:  L0 → L1 → - → Ln - 1 → Ln ...

  5. 143. Reorder List

    /** 143. Reorder List * 11.28 by Mingyang 总体思想就是后半部分reverse然后再merge*/public void reorderList(ListNod ...

  6. 【Leetcode】143. Reorder List

    Question: Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You ...

  7. LeetCode 143. 重排链表(Reorder List)

    题目描述 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. ...

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

  9. Leetcode 143. 重排链表

    难度:中等 频次:143 题目:给定一个单链表 L 的头节点 head ,单链表 L 表示为: 请将其重新排列后变为: 不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 解题思路:链表找中 ...

最新文章

  1. 图解VMWARE内存机制
  2. ubuntu php上传文件,Ubuntu中增加apache上传文件大小限制(突破Aapache默认2M上传限制)...
  3. jQuery 效果 - 滑动
  4. LeetCode Integer to Roman(数字转罗马)
  5. MATLAB实战系列(二十九)-头脑风暴优化(BSO)算法求解旅行商问题(TSP)-交叉算子
  6. xss Payload
  7. [零基础学JAVA]Java SE面向对象部分-08.面向对象基础(03)
  8. c++ websocket客户端_阿里面经WebSocket实时通信
  9. GuavaCache学习笔记二:Java四大引用类型回顾
  10. spark项目实战:电商分析平台之项目概述
  11. CentOS7 下配置 Nginx + PHP7 + MariaDB + ThinkPHP5.1
  12. 吴恩达深度学习笔记(八) —— ResNets残差网络
  13. Scala类型系统(sudden thought)
  14. c语言输入括号配对成功输出1,用数组实现括号配对检查,输出不正确,请大家指点解决办法...
  15. Google 工作十年后,我选择离开!
  16. 对自我认知四象限理解
  17. layim之邀请好友加入群组
  18. 我与电脑2-高中时期
  19. 【论文阅读笔记】Simple and Lightweight Human Pose Estimation
  20. P1386 座位安排

热门文章

  1. pytorch使用Ray-tune对原有训练模型的代码改写,自动调参(一)
  2. pandas获取最大值/最小值对应的index
  3. apt java8_Ubuntu 18.04安装Java JDK8三种方式
  4. oracle诊断日志,oracle日常诊断语句
  5. arduino 蓝牙示例_,arduino 蓝牙例子,
  6. Android浮窗权限研究(转载)
  7. Smack+Openfire 接收和发送文件
  8. 从偶然的机会发现一个mysql特性到wooyun waf绕过题
  9. 庆五一,We7同步发行2.5正式版、2.6 Beta版!
  10. [DP思考录]向左走,向右走: Observer模式 VS Mediator模式