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. 网站为什么要做SEO

    网站为什么要做seo,不做seo可以吗?因为seo是获得流量比较稳定.长久的方式,也是自身品牌的最好的方式.我们做的网站必须有用户访问或者被用户知道才有价值和意义,而想被用户所了解的话,必须做网络营销 ...

  2. HtmlAgilityPack解析器在WP8.1下报错,不仅如此,社交化分享也报错。

    以前WP7下是用的HtmlAgilityPack和 XPath来解析网页,很好用. 但是在Wp8.1下,这个里面却缺少了一个很重要的方法. HtmlDocument doc = new HtmlDoc ...

  3. nyoj 252 01串 动态规划( java)

    当n=2时, 输出 3:当n=3时, 输出 5:当n=4时, 输出 8: #### 分析: 当n=4时,如 0101 符合条件, 当第一个位置是0时,还剩3个位置 ,与n=3时个数相等: 符合条件的为 ...

  4. Codeforces Round #270 1001

    Codeforces Round #270 1001 A. Design Tutorial: Learn from Math time limit per test 1 second memory l ...

  5. 2013长沙赛区现场赛 J - Josephina and RPG

    J - Josephina and RPG Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  6. 关于学习session的一二

    session作为一种服务器端技术,在网页中与cookie相配合可谓相得益彰,今天学习了进一步session的相关知识,在这里记录一二,以便于以后查阅 /*问题:当我们浏览网页的时候,张三和李四可以购 ...

  7. ios 客户端定位的3种方法

    1.客户端通过IP地址定位 http://int.dpool.sina.com.cn/iplookup/iplookup.php 输出:城市 1 -1 -1 中国 广东 深圳 2.客户端获取经纬度,调 ...

  8. linux之vim编辑神器

    设置编辑器:export EDITOR=vim 1.ctrl+x ctrl+e  创建vim临时文件 2.ctrl+[ 退回到命令模式 3.命令模式下ZZ,保存退出 4.大写I行首插入,大写A行尾插入 ...

  9. java中的方法重载与重写以及方法修饰符

    1. 方法重载Overloading , 是在一个类中,有多个方法,这些方法的名字相同,但是具有不同的参数列表,和返回值 重载的时候,方法名要一样,但是参数类型和参数个数不一样,返回值类型可以相同,也 ...

  10. html5拖拽实现

    1.需求 做一个h5正方形的拖拽框 2.分析 使用touchstart,touchmove,touchend这3个事件实现. 需要记录的数据有三组数据,分别是下图的(x0,y0),(x1,y1),(x ...