Reorder List

Given a singly linked list L: L0L1→…→Ln-1Ln, reorder it to: L0LnL1Ln-1L2Ln-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;
ListNode *p1, *p2, *p3, *head2;
p1 = p2 = head;
while(p2->next && p2->next->next) {
p1 = p1->next;
p2 = p2->next->next;
}
head2 = p1->next; p1 = p1->next = NULL;
p2 = head2;
while(p2) {
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head2 = p1; p1 = head;
while(head2 && p1) {
p2 = head2->next;
head2->next = p1->next;
p1->next = head2;
head2 = p2;
p1 = p1->next->next;
} }
};

13. Reorder List的更多相关文章

  1. LeedCde 题解目录

    1. Longest Palindromic Substring ( 最长回文子串 ) 2. Median of Two Sorted Arrays (两个排序数组的中位数) 3. Sqrt(x) 4 ...

  2. shell编程基础(七): 处理文件命令sed与awk

    一.sed(以行为单位处理文件) sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用非常普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑 ...

  3. [developmemt][dpdk] dpdk优化(转)

    转发:https://software.intel.com/en-us/articles/dpdk-performance-optimization-guidelines-white-paper 转发 ...

  4. dpdk优化相关 转

    注:本文是参照了一些其他文章,原文地址点击这里. 首先根据这篇文章进行了性能瓶颈的分析 策略与方法 首先根据木桶原理,首先要找到最弱的地方,怎么找往上看↑. 想能优化需要考虑如下: 优化BIOS设置 ...

  5. leetcode 题型 数据结构 解法 分类总结

    第2章 线性表 2.1 数组 2.1.1 Remove Duplicates from Sorted Array 2.1.2 Remove Duplicates from Sorted Array I ...

  6. 62. 链表重排[Reorder List]

    [本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...

  7. TechEmpower 13轮测试中的ASP.NET Core性能测试

    应用性能直接影响到托管服务的成本,因此公司在开发应用时需要格外注意应用所使用的Web框架,初创公司尤其如此.此外,糟糕的应用性能也会影响到用户体验,甚至会因此受到相关搜索引擎的降级处罚.在选择框架时, ...

  8. .NET平台开源项目速览(13)机器学习组件Accord.NET框架功能介绍

    Accord.NET Framework是在AForge.NET项目的基础上封装和进一步开发而来.因为AForge.NET更注重与一些底层和广度,而Accord.NET Framework更注重与机器 ...

  9. 转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38

    转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38http://space.itpub. ...

随机推荐

  1. ASIHTTPRequest下载示例(支持断点续传)

    一.创建网络请求队列 首先,创建网络请求队列,如下: ASINetworkQueue   *que = [[ASINetworkQueue alloc] init]; self.netWorkQueu ...

  2. jdk6提供的加密算法

    SUN:SHA1PRNG____sun.security.provider.SecureRandomSUN:SHA1withDSA____sun.security.provider.DSA$SHA1w ...

  3. java string.format()

    String text=String.format("$%1$s 门市价:¥%2$s",18.6,22);$18.6 门市价:¥22

  4. JS的(function($){})(query)

    function(arg){...} 这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写上括号和实参的,由于操作符的优先级,函数本身也需要用括号,即:(function(arg){.. ...

  5. ABOUT LIFETIME

    This is where we started We've come a long way since our beginning. It all started as an idea in a g ...

  6. HDU1016 dfs

    刷回溯的时候发现我对DFS思路很不清晰,总是做着做着就乱了,刷个水题找找思路. 题意:经典DFS,找出所有的能让1~n的数形成素数环的序列(相邻相加为素数): #include <iostrea ...

  7. php之无限极分类

    首先建立分类信息表: CREATE TABLE IF NOT EXISTS `category` ( `categoryId` smallint(5) unsigned NOT NULL AUTO_I ...

  8. 问:Linux下Chrome标题栏中文乱码

    From:http://blog.csdn.net/loveaborn/article/details/29579787 在使用Linux的时候你会遇到一些奇奇怪怪的问题,不过,你会在解决这些问题的过 ...

  9. toad 9.6和toad 12.1工具使用比较

    toad是我工作中使用最频繁的软件之一,阴错阳差的把2个版本都装到了电脑上,使用过程中逐渐发现2者的差异,特此做下记录,以便提示自己和其他有需要的人们.(随时更新中······)由于能力有限,难免会有 ...

  10. aptitude解决Ubuntu各种依赖问题

    转自:http://allog.ml/linux/aptitude%E8%A7%A3%E5%86%B3ubuntu%E5%90%84%E7%A7%8D%E4%BE%9D%E8%B5%96%E9%97% ...