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. /Home/Tpl/Equipment/rangeIndex.html 里调用魔板

    <pre name="code" class="html">demo:/var/www/DEVOPS# vim ./Home/Tpl/Equipme ...

  2. GitHub上线Trending功能,帮你轻松找到有潜力的开源项目

    转自:http://www.csdn.net/article/2013-08-14/2816574-Github-Trending-Open-Source-Project Github开源项目 摘要: ...

  3. github上排名靠前的java项目之_storm

    1.和hadoop的比较 Storm: 分布式实时计算,强调实时性,常用于实时性要求较高的地方 Hadoop:分布式批处理计算,强调批处理,常用于数据挖掘.分析   2.Hadoop是实现了mapre ...

  4. 【模拟】Vijos P1005 超长数字串

    题目链接: https://vijos.org/p/1005 题目大意: 无限的正整数按顺序拼接成字符串S(S=12345678910111213...),给你一个字符串A(len<=200)求 ...

  5. [Locked] Binary Tree Upside Down

    Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...

  6. HDU2059(龟兔赛跑)

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  7. poj2255

    题目大意: 树恢复??树复原?? 小Valentine非常喜欢玩二叉树的游戏,他非常喜欢在二叉树的树根上随机的写上一下大写字母,这是她创造的一个例子: D / \ / \ B E / \ \ / \ ...

  8. CodeForces 27D - Ring Road 2 构图2-sat..并输出选择方案

        题意             n个数1~n按顺序围成一个圈...现在在某些两点间加边..边可以加在圈内或者圈外..问是否会发生冲突?如果不发生冲突..输每一条边是放圈内还是圈外.     题解 ...

  9. 【每日一linux命令8】添加新的工作组(groupadd)

    groupadd (字意add group)增加一个新的工作组. 语法:groupadd 选项 用户组名 选项: -g 指定新建工作组的ID -r 创建系统工作组,系统工作组的ID小于500 -k 覆 ...

  10. [置顶] Android JNI必须掌握的五点

      1:JNI是什么? Java NativeInterface(JNI)是Java提供的一个很重要的特性.它使得用诸如C/C++等语言编写的代码可以与运行于Java虚拟机(JVM)中的 Java代码 ...