【本文链接】

http://www.cnblogs.com/hellogiser/p/reorder-list.html

【题目】

Given a singly linked list L: L0→L1→…→Ln-1→Ln,

reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes' values.

For example,

Given {1,2,3,4,5,6,7}, reorder it to {1,7,2,6,3,5,4}.

【分析】

题目思路比较直接:

(1)找到链表的中间节点,把链表划分成2个子链表;如果原链表长度为奇数,那么第一个子链表的长度多1;

(2)翻转第二个子链表;

(3)交叉合并两个子链表。

例如{1,2,3,4,5,6,7}

(1)找到链表的中间节点为4,把链表划分成2个子链表:{1,2,3,4}和{5,6,7};

(2)翻转第二个子链表得到{7,6,5}

(3)交叉合并{1,2,3,4}和{7,6,5}得到{1,7,2,6,3,5,4}

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 
// 62_ReorderList.cpp : Defines the entry point for the console application.
//
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/5/30
*/
#include "stdafx.h"

struct ListNode
{
    int value;
    ListNode *next;
};

// find middle node of list
ListNode *FindMiddleNode(ListNode *head)
{
    if(NULL == head)
        return NULL;
    ListNode *fast = head, *slow = head;
    while(fast != NULL && fast->next != NULL)
    {
        // move fast 2 steps
        fast = fast->next->next;
        if (fast == NULL)
            break;
        // move slow 1 step
        slow = slow->next;
    }
    return slow;
}

// reverse list
ListNode *ReverseList(ListNode *head)
{
    if(NULL == head || NULL == head->next)
        return head;
    ListNode *prev = NULL, *cur = head, *next = NULL;
    while(cur != NULL)
    {
        // save next
        next = cur->next;
        // reverse
        cur->next = prev;
        // update prev and cur
        prev = cur;
        cur = next;
    }
    return prev;
}

// cross merge list
ListNode *CrossMergeList(ListNode *head1, ListNode *head2)
{
    if(NULL == head1)
        return head2;
    else if (NULL == head2)
        return head1;

ListNode *node1 = head1, *node2 = head2;
    while(node2 != NULL)
    {
        ListNode *temp1 = node1->next;
        ListNode *temp2 = node2->next;
        node1->next = node2;
        node2->next = temp1;
        // update node1 node2
        node1 = temp1;
        node2 = temp2;
    }
    return head1;
}

// reorder list
ListNode *ReOrderList(ListNode *head)
{
    if(NULL == head || NULL == head->next)
        return head;

// find middle node of list
    ListNode *middle = FindMiddleNode(head);
    // split into 2 lists
    ListNode *head1 = head;
    ListNode *head2 = middle->next;
    // detach the 2 lists
    middle->next = NULL;
    // reverse list2
    head2 = ReverseList(head2);
    // cross merge 2 lists
    return CrossMergeList(head1, head2);
}

【参考】

http://blog.csdn.net/whuwangyi/article/details/14146461

【本文链接】

http://www.cnblogs.com/hellogiser/p/reorder-list.html

62. 链表重排[Reorder List]的更多相关文章

  1. 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...

    链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...

  2. LeetCode 143. 重排链表(Reorder List)

    题目描述 给定一个单链表 L:L0→L1→…→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. ...

  3. LeetCode之“链表”:Reorder List

    题目链接 题目要求: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You ...

  4. 【链表】Reorder List

    题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do ...

  5. Leetcode0143--Reorder List 链表重排

    [转载请注明]https://www.cnblogs.com/igoslly/p/9351564.html 具体的图示可查看 链接 代码一 /** * Definition for singly-li ...

  6. 一篇文章搞定面试中的链表题目(java实现)

    最近总结了一下数据结构和算法的题目,这是第二篇文章,关于链表的,第一篇文章关于二叉树的参见 废话少说,上链表的数据结构 class ListNode { ListNode next; int val; ...

  7. [译]Memory Reordering Caught in the Act

    原文:http://preshing.com/20120515/memory-reordering-caught-in-the-act/ 编写lock-free的C/C++程序时,在保证memory  ...

  8. HashMap原理阅读

    前言 还是需要从头阅读下HashMap的源码.目标在于更好的理解HashMap的用法,学习更精炼的编码规范,以及应对面试. 它根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而 ...

  9. 深度剖析HashMap的数据存储实现原理(看完必懂篇)

    深度剖析HashMap的数据存储实现原理(看完必懂篇) 具体的原理分析可以参考一下两篇文章,有透彻的分析! 参考资料: 1. https://www.jianshu.com/p/17177c12f84 ...

随机推荐

  1. 【 Jquery插件】引导用户如何操作网站功能的向导

    Joyride是一个jQuery插件,可以利用它来创建一个引导用户如何操作网站功能的向导.通过定义一个操作步骤顺序,这个插件会在需要操作的HTML元素旁边显示一个帮助说明的Tooltips. http ...

  2. c语言的数学函数ceil、floor、round

    头文件<math.h> 函数原型和作用 double ceil(double x); 向上取整 double floor(double x); 向下取整 double round(doub ...

  3. NOI题库--砝码称重V2(多重背包2^n拆分)

    以前只会写多重背包的原版,渣的不行,为了做此题不得不学习了一下,发现其实也不难,只要理解了方法就好多了(PS:其实和倍增挺像的) 8756:砝码称重V2 总时间限制: 1000ms 内存限制: 655 ...

  4. TYVJ1305 最大子序和

    描述 输入一个长度为n的整数序列,从中找出一段不超过M的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7当m=2或m=3时,S=5+1=6 输入 ...

  5. 压力测试工具tsung

    tsung是用erlang开发的一款简单易用的压力测试工具,可以生成成千上万的用户模拟对服务器进行访问.目前对tsung的理解也仅限于会简单的应用,其内部结构没有深入研究过. 1.安装 tsung是用 ...

  6. Android——Canvas类的学习

    转:http://blog.sina.com.cn/s/blog_61ef49250100qw9x.html 今晚瞎折腾,闲着没事画了个机器人——android,浪费了一个晚上的时间.画这丫还真不容易 ...

  7. Codeforces 295A Greg and Array

    传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...

  8. zoj3819Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  9. MyEclipse------快速写入内容到指定目录下的文件(字节输出流)

    other.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  10. xml转成数组,原来这么简单!

    function xml2arr($xml){ $obj  = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $js ...