题目:

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

  You must do this in-place without altering the nodes' values.

  For example,
    Given {1,2,3,4}, reorder it to {1,4,2,3}.

解题思路:

  1、用快慢指针找到链表的中点,并将链表分割成两部分

  2、翻转后面的部分

  3、将前后两部分拼接

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:void reorderList(ListNode *head) {if (head == NULL) return;//find the last partListNode *mid = head, *last = head;while (last != NULL) {last = last->next;if (last == NULL) break;last = last->next;mid = mid->next;}ListNode *right_head = mid->next;mid->next = NULL;//翻转 右边部分ListNode *pre = NULL, *suf = right_head;while (suf != NULL) {ListNode * tmp = suf->next;if (tmp == NULL) {right_head = suf;}suf->next = pre;pre = suf;suf = tmp;}//拼接ListNode *cur = head;while (cur != NULL && right_head != NULL) {ListNode *tmp = right_head->next;right_head->next = cur->next;cur->next = right_head;right_head = tmp;cur = cur->next->next;}}
};

转载于:https://www.cnblogs.com/dongguangqing/p/3726345.html

LeetCode OJ - Reorder List相关推荐

  1. LeetCode OJ -- Binary Tree Paths

    http://blog.ubooksapp.com/ 标签(空格分隔): LeetCode OJ BinaryTree Given a binary tree, return all root-to- ...

  2. 【重点】LeetCode 143. Reorder List

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

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

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

  5. LeetCode OJ:Valid Anagram(有效字谜问题)

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  6. 【LeetCode OJ 136】Single Number

    题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...

  7. LeetCode OJ -Happy Number

    题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...

  8. leetcode day5 -- Reorder List Linked List Cycle II

    1.  Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln ...

  9. [Leetcode][JAVA] 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 ...

最新文章

  1. 网络管理常用命令之二 - Ipconfig 命令详解(图文)
  2. LPC55S69 MicroPython模组和库函数
  3. LeetCode Intersection of Two Arrays
  4. linux内核杂记(3)-进程(1)
  5. PHP连接达梦数据库
  6. Hibernate学习笔记之EHCache的配置
  7. 简单servlet和jdbc回顾
  8. Asp.net文章内容分页
  9. ImageNet dataset下载
  10. Maven -- dependency详解
  11. 数据工程师必须掌握的7个大数据实战项目
  12. 51单片机学习笔记【九】——红外通信实验
  13. python教学反思_Python语言教学反思
  14. 支付宝接口的在线支付(https://openhome.alipay.com/platform/appDaily.htm?tab=info)
  15. 删除Excel数据中的空格
  16. 两年后,我仍然想念耳机端口
  17. 眼见为实:被误导的Tomcat工作原理
  18. matlab 清除命令,清空工作区代码,关闭所有窗口
  19. vue和socket.io开发简单web聊天室
  20. Token一般存放在哪里

热门文章

  1. android 布局中的单位及分辨率自解
  2. li中浮动元素span等在IE和Firefox中的高度Bug
  3. LeetCode(1030)——距离顺序排列矩阵单元格(JavaScript)
  4. 联想340c笔记本cpu能升级吗_高性价比十大笔记本电脑(由高到低价格),有一台五五开也有.........
  5. python柱状图代码_python+matplotlib实现礼盒柱状图实例代码
  6. 电脑内存16g和32g,有什么区别?
  7. 如果现在单位让你内退,拿基本工资,不用上班,你是否愿意?
  8. 父亲购房后去世,房产证还没办,后妈和父亲没结婚,户口簿上有父亲,儿子,妹妹,那房产证应该写谁的名字?
  9. 在承德避暑山庄里待过,给你的最大感受是什么?
  10. 选什么专业?找什么样的工作?教你四个维度