For example,
Given {1,2,3,4}, reorder it to {1,4,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) {
//求链表的总长度len
if(head==NULL)
return;
int len=;
ListNode *p = head; //tail用于指向整个链表的尾节点
while(p!=NULL)
{
p = p->next;
len++;
}
//将链表拆成两半,并将后半部分的链表顺序倒置,如原来链表为L1->L2->L3->L4->L5->L6->L7,现在得到两个链表L1->L2->L3->L4和L7->L6->L5
int i=;
ListNode * h1 = head;
ListNode * h2 = head ,*pre;
while(i<(len+)/)
{
pre = h2;
h2 = h2->next;
i++;
}
pre->next = NULL; //h1指向拆分后得到的第一个链表的第一个节点,并将第一个链表的最后一个节点的指针域置为NULL; h2指向拆分后得到的第二个链表的第一个节点,到这一步尚未对第二个链表倒置 //对第二个链表进行倒置
ListNode * temp;
p = h2;
if(p!=NULL)
{ h2 = h2->next;
p->next = NULL;
while(h2!=NULL)
{
temp = h2;
h2 = h2->next;
temp->next = p;
p = temp;
}
}
h2 = p; //由两个链表L1->L2->L3->L4和L7->L6->L5按如下方式得到所求的第三个链表,将第一个链表的第一个节点连接到第三个链表的末端,再将第二个链表的第一个节点连接到第三个链表的末端,以此类推,直到两个链表都为空
//得到的第三个链表便是L1->L7->L2->L6->L3->L5->L4
ListNode * tail = h1;
h1 = h1->next;
i = ;
while(h1!=NULL || h2!=NULL)
{
tail->next = (++i%)? h2 : h1;
tail = tail->next;
(i%) ? (h2 = h2->next) : (h1 = h1->next);
}
return;
}
};

[LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…的更多相关文章

  1. LeetCode OJ:Delete Node in a Linked List(链表节点删除)

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  2. LeetCode OJ 之 Delete Node in a Linked List (删除链表中的结点)

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  3. 【LeetCode OJ】Flatten Binary Tree to Linked List

    Problem Link: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ The problem is ask ...

  4. LeetCode OJ 114. Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  5. LeetCode OJ:Flatten Binary Tree to Linked List(捋平二叉树)

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  6. LeetCode OJ 143. 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 thi ...

  7. LeetCode OJ: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 thi ...

  8. 【LeetCode OJ】Linked List Cycle

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle/ We set two pointers: the faster poi ...

  9. &lt;LeetCode OJ&gt; 328. Odd Even Linked List

    328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...

随机推荐

  1. 2015第40周一Node学习

    node学习尝试 早上看了张丹大牛博客文章nodeJS学习路线图和node从零入门系列,感觉获益匪浅,尝试了里面几项内容,对node有了更深入的认识. npm npm是一个node包管理和分发工具,已 ...

  2. 使用XRDP实现Windows远程桌面Linux系统

    一般情况下我们用ssh客户端远程登陆Linux系统,至于图形界面下的linux远程登陆工具,我们一般都会想到vnc,但它的安全性不够,在这里,我将介绍XRDP的安装配置方法.我们可以很方便的通过Win ...

  3. geopy使用详解

    由于专业需要,经常接触一些地理处理的工具包,文档都是英文的,自己看的同时将其翻译一下,一方面自己学习的同时有个记录,要是能同时给一起的学习的童鞋们一些帮助,想想也是极好的.以下的文档内容主要翻译自官方 ...

  4. max Sum(简单动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 / 给组测试数据 1 7 2 3 -4 -5 6 7 8 一个关键问题 : 什么时候将开始位置重新赋值 即 ...

  5. [C++关键字] alignof & alignas 内存对齐 sizeof 占内存大小

    直接上代码测试是入门神器,以结构体为例,解释“对齐”和“补齐”概念. #include <iostream> struct Empty {}; struct Foo { int f2; d ...

  6. POJ 2912 Rochambeau

    题意:有一些人玩石头剪刀布,其中有一个人(称其为裁判)可以出“石头”,“剪刀”,“布”中的任意一个,其他人永远只能出相同的一个.即有的人只能出剪刀,有的人只能出石头,有的人只能出布.进行了多次对决,每 ...

  7. 《C专家编程》读书笔记

    第一章 const float* 表示一个指向float类型常量的指针 第二章 1. 在c语言中const并非真正表示“常量”,在数组定义与case中不可以使用 2. case的一些问题 1: #in ...

  8. bind()

    首先是改变this指向问题 var altwrite = document.write; altwrite("hello"); 上面的程序运行,会报错:Uncaught TypeE ...

  9. 发起SSH攻击主机IP地址列表

    发起SSH攻击主机IP地址列表 东北大学 http://antivirus.neu.edu.cn/scan/ssh.php 以下IP地址对SSH服务进行攻击,严重增加主机成为肉鸡的可能性.强烈建议网管 ...

  10. 页游AS客户端架构设计历程记录

    以下是一个只用JAVA做过服务器架构的程序员做的AS客户端架构,希望大家能推荐好的框架和意见,也求AS高程们的引导,等到基本功能成形后,低调开源,框架可以支持一个中度型页游的开发,本文不断更新中... ...