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. Maven实战五

    转载:http://www.iteye.com/topic/1123232 我们项目中用到的jar包可以通过依赖的方式引入,构建项目的时候从Maven仓库下载即可. 1. 依赖配置    依赖可以声明 ...

  2. JButton 做图片框

    JButton setHorizontalTextPosition(SwingConstants.CENTER);// 在水平方向文字位于图片中央 setVerticalTextPosition(Sw ...

  3. w​i​n​d​o​w​s​7​旗​舰​版​I​I​S​6​配​置​-​保​证​能​发​布

    http://wenku.baidu.com/view/20b4d26248d7c1c708a145d1.html

  4. UVA 11922 Permutation Transformer(Splay Tree)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 [思路] 伸展树+打标记. 用伸展树维护这个序列,使得能 ...

  5. Best Reward HDU 3613(回文子串Manacher)

    题目大意:有一个串(全部由小写字母组成),现在要把它分成两部分,如果分开后的部分是回文串就计算出来它的价值总和,如果不是回文的那么价值就是0,最多能得到的最大价值.   分析:首先的明白这个最大价值有 ...

  6. H - Cow Contest

    有N头牛,编号从1到N,参与一个变成比赛(大牛编程比赛,一般水水平敢参加???),一些牛的代码比较出色,每头牛都有一个独一无二的技能等级在这些竞争者中. 比赛循环进行在任意两头牛之间(姑且这么翻译吧) ...

  7. spring mvc mybatis

    Spring与Mybatis整合需要引入一个mybatis-spring.jar包,该整合包有Mybatis提供,可以从Mybatis官网下载. 该jar包提供了几个API: 1.SqlSession ...

  8. 快速了解常用XHTML基础

    运行效果: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  9. js输入框只能输入数字和小数点

    <input name="number" onKeyPress="if (event.keyCode!=46 && (event.keyCode&l ...

  10. Fuel4D 2.1 免费跨平台游戏引擎 现已发布

    Fuel4D 2.1 免费跨平台游戏引擎 现已发布 开发环境:纯 C/C++,无JAVA.O-C. 支持 WIN32.安卓 系统. 详情见官方网站:http://www.fuel4d.com 或者进F ...