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

思路:

先把链表分成两节,后半部分翻转,然后前后交叉连接。

大神的代码比我的简洁,注意分两节时用快慢指针。

大神巧妙的在最后一步融合时用了连等号

在翻转部分:大神翻转过的部分的结尾是null. 而我的方法是把结尾连接下一个待翻转的结点。

    // O(N) time, O(1) space in total
void reorderList(ListNode *head) {
if (!head || !head->next) return; // find the middle node: O(n)
ListNode *p1 = head, *p2 = head->next;
while (p2 && p2->next) {
p1 = p1->next;
p2 = p2->next->next;
} // cut from the middle and reverse the second half: O(n)
ListNode *head2 = p1->next;
p1->next = NULL; p2 = head2->next;
head2->next = NULL;
while (p2) {
p1 = p2->next;
p2->next = head2;
head2 = p2;
p2 = p1;
} // merge two lists: O(n)
for (p1 = head, p2 = head2; p1; ) {
auto t = p1->next;
p1 = p1->next = p2;
p2 = t;
}
}

我的代码

void reorderList(ListNode *head) {
int len = ; //链表长度
ListNode * p = head;
ListNode * latterpart = head;
//找链表长度
while(p != NULL)
{
len++;
p = p->next;
} if(len <= )
{
return;
} //把链表分成两份 如1 2 3 4 5 分成 1 2 3 和 4 5
len = (len + ) / ; //一半的位置
p = head;
while(--len)
{
p = p->next;
}
latterpart = p->next;
p->next = NULL; //翻转后半部分
ListNode * plast = latterpart;
while(plast->next != NULL)
{
p = plast->next;
plast->next = p->next;
p->next = latterpart;
latterpart = p; //更新头部 每次把后面的转到最前面去
} //交叉前后两段
p = head;
while(p != NULL && latterpart != NULL) //如果前半部分和后半部分都还有可连接的 继续
{
ListNode * tmp = p->next;
p->next = latterpart;
latterpart = latterpart->next;
p->next->next = tmp;
p = p->next->next;
} return;
}

【leetcode】Reorder List (middle)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  3. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  4. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  6. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  7. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  9. 【leetcode】Surrounded Regions(middle)☆

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

随机推荐

  1. 2015年11月26日 Java基础系列(四)class的定义,继承和实现interface

    序,类的设计是JAVA操作的核心,面对对象思想中一切皆对象. 一.类定义中的变量 静态成员变量,为类所有,称为类变量:只有一份,编译时即分配值,使用关键字static声明. 非静态成员变量,每个实例一 ...

  2. Linux网络参数设置

    1.ifconfig  查询.设定网络卡与ip     设置桥接网络 # vi /etc/sysconfig/network-script/ifcfg-br0       DEVICE=br0     ...

  3. Windows 操作小技巧 之一(持续更新)

    1.图片批量旋转 通常携带单反去景点排了大量照片回来处理图片时都会遇到很多横竖杂乱排序的图片难以处理的情形.现提供如下技巧进行处理. 1).在文件夹中添加"方向"的排列或分组选项: ...

  4. STM32F103xx bxCAN(Basic Extended CAN) 滤波机制

    一.背景 最近一个项目需要使用STM32F103xx实现CAN通信,而CAN总线的消息滤波在各个MCU上有不同机制, 譬如,SJA1000为标识符位屏蔽滤波机制,NXP的LPC17xx系列为标识符列表 ...

  5. 第24天 runtime

    面试时被问到一个问题,如何实现weak变量的自动置nil?当时也不知道. 今天在实现target-action模式时,如何调用SEL,刚开始只会PerformSelector,但不能传递多个参数,后来 ...

  6. ASN.1编码

    来自几年前本人写的一篇博客 http://blog.csdn.net/newyf_cun/article/details/13016069 如下使用libtasn1分析asn1的编码规则. http: ...

  7. updatepanel用法之triggers(局部刷新,全部刷新)使用示例

    asyncpostbacktrigger(异步回调触发器):局部刷新,只刷新updatepanel内部的内容postbacktrigger(普通回调触发器):全部刷新 <asp:ScriptMa ...

  8. BZOJ2780——[Spoj]8093 Sevenk Love Oimaster

    0.题意:给定N个原始字符串S,M次查询某个特殊的字符串S'在多少个原始串中出现过. 1.分析:这个题我们第一感觉就是可以用后缀自动机来搞,然后我们发现不是本质不同的字串..求出现过的次数,也就是说多 ...

  9. JavaScript深入浅出2-表达式和运算符

    慕课网教程视频地址:Javascript深入浅出 表达式是指能计算出值的任何可用程序单元 原始表达式:常量.直接量 3.14,“test” 关键字 null,this 变量 i,k,j 表达式含:原始 ...

  10. 如何安装PANABIT?

    PANABIT可以帮你轻松的封杀360WIFI.二级路由和移动设备,还可以让员工在上班期间禁止浏览与工作无关的网站,禁止看视频,禁止聊QQ,禁止网购......总之一切你能想到的,它都能帮你实现. P ...