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

解题思路:

设置一个指针mid指向链表的中间结点;

将mid之后的半个链表进行反转,反转后的链表insert;

将insert中的结点挨个插入前半段链表中;

代码:

 /**
* 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 || !head->next || !head->next->next)
return;
ListNode* ctrl = head;
ListNode* mid = head;
ListNode* insert = NULL; while (ctrl->next && ctrl->next->next) {
ctrl = ctrl->next->next;
mid = mid->next;
} insert = reverseList(mid->next);
mid->next = NULL;
ctrl = head; while (insert) {
ListNode* t = ctrl->next;
ctrl->next = insert;
insert = insert->next;
ctrl->next->next = t;
ctrl = t;
} return;
} ListNode* reverseList(ListNode* head) {
if (!head->next)
return head;
ListNode* cur = NULL;
ListNode* next = head;
ListNode* left = NULL;
while (next) {
left = next->next;
next->next = cur;
cur = next;
next = left;
}
return cur;
}
};

【Leetcode】【Medium】Reorder List的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  6. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  7. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  8. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  9. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  10. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

随机推荐

  1. 问题记录——com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    最近在搞一个Spring boot + Mybatis + Mysql的项目,用Mybatis访问数据库时,报了如下的错误,先在网上搜索了,试了各种办法都不行, 奇葩的是,连接另外1个数据库又没问题. ...

  2. atoi和stoi

    vs环境下:stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error.atoi函数则不做范围检查,若超过int范围,则 ...

  3. MySQL修改提示符

    MySQL提示符 \D 完整日期 \d 当前数据库 \h 服务器名称 \u 当前用户 1.连接之前修改提示符 mysql -uroot -proot --prompt [MySQL提示符] 2.连接之 ...

  4. 参数化登录QQ空间实例

    通过参数化的方式,登录QQ空间 实例源码: # coding:utf-8 from selenium import webdriver import unittest import time clas ...

  5. LinuxShell脚本编程基础5--数值,字符串,文件状态测试,((..))和[[..]]的使用

    1.数值比较 ! /bin/bash echo "enter a score:" read num1 ] then echo "Very Good" elif ...

  6. 配置vim插件遇到youcompleteme插件问题解决方案

    今天在Opensuse下配置vim 遇到两个问题 配置插件找到一个很好的博客.学到一些有用技巧 http://hahaya.github.io/2013/07/26/use-vundle.html 但 ...

  7. 【CSS】 布局之浮动float和绝对定位absolute的选择

    浮动float: 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样.(W3C) 绝对定位 ...

  8. nexus开机启动

    在/etc/init.d目录下创建nexus文件 #!/bin/bash #chkconfig: #description:nexus3 #processname:nexus3 export JAVA ...

  9. jQuery 的动画效果图片----隐藏打开方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. java gc 随记

    gc为garbage collection的缩写,中文翻译为垃圾回收.垃圾为不在使用的实例.变量,回收为释放垃圾所占用的内存空间. 学习过的C语言.C++语言,是没有垃圾回收机制的,因此需要软件工程师 ...