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. Maven初级学习(二)Maven使用入门

    序,学习配置pom.xml,利用maven生成eclipes项目. 一.编写POM POM Project Obejct Model,项目对象模型. 编写pom.xml,新建文件夹hello-worl ...

  2. c#winform选择文件,文件夹,打开指定目录方法

    private void btnFile_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDia ...

  3. 深入理解计算机系统-从书中看到了异或交换ab两个值的新感

    还得从一个很经典的面试题说起:不通过第三个变量来交换两个变量a,b的值... 一个很经典的答案是通过异或来解决: 第壹步:a=a^b; 第贰步:b=a^b; 第叁步:a=a^b; 以前提起" ...

  4. HDOJ 4747 Mex

    非常好的线段树题....此题必定会火..... Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K ( ...

  5. hibernate中get,load,list,iterate的用法及比较

    首先,get和load都是查询单个对象,而list和iterate为批量查询 注意以下写法仅针对hibernate3的语法. 使用案例如下: // 1. get和load 的用法 Person p = ...

  6. Java系列笔记(4) - JVM监控与调优

    目录 参数设置收集器搭配启动内存分配监控工具和方法调优方法调优实例     光说不练假把式,学习Java GC机制的目的是为了实用,也就是为了在JVM出现问题时分析原因并解决之.通过学习,我觉得JVM ...

  7. Cache and Virtual Memory

    Cache存储器:电脑中为高速缓冲存储器,是位于CPU和主存储器DRAM(DynamicRandonAccessMemory)之间,规模较小,但速度很高的存储器,通常由SRAM(StaticRando ...

  8. HDU 1532 最大流模板题

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最近在学网络流,学的还不好,先不写理解了,先放模板... 我觉得写得不错的博客:http://blo ...

  9. Android应用签名

    http://www.cnblogs.com/ghj1976/archive/2011/07/18/2109381.html 为了要签名? 开发Android的人这么多,完全有可能大家都把类名,包名起 ...

  10. ADB连不上,ADB server didn't ACK问题,的解决

    adb连接不上虚拟机,即便执行了adb kill-server命令也没用? 如果你的错误信息是,ADB server didn't ACK这样的话,说明5037端口被占用了. 只要找出占用此端口的进程 ...