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. selenium+Python(事件)

    1.操作测试对象前面讲到了不少知识都是定位元素,定位只是第一步,定位之后需要对这个元素进行操作.鼠标点击或者键盘输入,这要取决于我们定位的是按钮还输入框.一般来说,webdriver 中比较常用的操作 ...

  2. 【随笔】Linux主机简单判断CC攻击的命令

    今天看到一个很有意思的命令tcpdump,在这里记录下. 如果想要看tcpdump的详细用法,可以点击这里. 什么是CC攻击? 关于CC攻击,这里引用百度的解释: CC攻击的原理就是攻击者控制某些主机 ...

  3. PHP正则表达式(转载)

    这个星期要攻破PHP正则表达式   正则表达式定义 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取 ...

  4. C++的引用本质上等同于C的指针

    本文给出一个很小的例子,通过反汇编后观察认为,C++的引用本质上等同于C的指针. o foo1.cpp v.s. foo2.cpp o 对foo1.cpp 和foo2.cpp进行编译 $ g++ -g ...

  5. linux多线程同步

    1. 互斥量是线程同步的一种机制,用来保护多线程的共享资源.同一时刻,只允许一个线程对临界区进行访问.互斥量的工作流程:创建一个互斥量,把这个互斥量的加锁调用放在临界区的开始位置,解锁调用放到临界区的 ...

  6. hdu 3794 Magic Coupon

    浙大计算机研究生保研复试上机考试-2011年  贪心: 注意:输入输出用scanf  printf 可以加快速度,用cin WA #include<iostream> #include&l ...

  7. 前端渲染模板(一):Thymeleaf

    一.使用 本篇文章将以SpringBoot为框架来介绍Thymeleaf的用法. 1 资源文件的约定目录结构  Maven的资源文件目录:/src/java/resources spring-boot ...

  8. WAMP环境配置-PHP安装

    我这次环境配置安装的是php-5.6.25版本! (最近我在反复安装PHP的时候出现了一个问题,httpd.conf加载php5apache2_4.dll出现错误,怎么修改都不行,此时我安装的是VC1 ...

  9. 理解Xaml标记语言

    理解XAML XAML基于XAML,因而具有与XAML相似的特性.在XAMl中,同样必须区分大小写,但是Xaml以.xaml作为扩展名,表示这是一个应用程序的标记扩展文件.WPF中的XAML主要用于创 ...

  10. .Net程序员玩转Android系列之二~Android Framework概要(1)

    从windows操作系统说起 人们总是喜欢从将陌生的事物和自己所了解的东西关联起来,以加深对未知事物的了解,这一讲我们从windows操作系统说起,逐步引领带大家走入android的世界.写任何程序都 ...