[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→…
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→…的更多相关文章
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【LeetCode OJ】Linked List Cycle
Problem link: http://oj.leetcode.com/problems/linked-list-cycle/ We set two pointers: the faster poi ...
- <LeetCode OJ> 328. Odd Even Linked List
328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...
随机推荐
- struts2支持的结果处理类型
有些东西常常在用,自己却忽略总结,这是一个特别的失误. struts2支持的结果处理类型 struts2的结果处理类型要在struts-default.xml文件中的<result-typs&g ...
- Linux负载均衡软件LVS之一(概念篇)
一. LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,它的官方站点是www.linuxvirtualserver ...
- 更改Visual Studio 2010/2012/2008的主题设置
一.更改主题: 主题网站:http://studiostyl.es/ Visual Studio 2010发布也已经有一段时间了,不过安装后默认的白底的主题长时间看代码可能会感觉眼睛酸痛,况且时间长了 ...
- weblogic jsp 不生效解决方法
1. 检查weblogic.xml配置文件,其中如果有: <jsp-descriptor> <jsp-param> <param-name>pageCheckSec ...
- 【模拟】Codeforces 710C Magic Odd Square
题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. ...
- 开源的excel读取库libxls在windows下的编译,且支持中文,全网首发
转载请注明出处:http://www.cnblogs.com/superbi/p/5482516.html 到目前为止,网络和官网上并没有关于libxls在windows下支持中文的教程,也没有现成的 ...
- WCF分布式事务
原文地址:http://developer.51cto.com/art/201002/185426.htm 我们作为一个开发人员,应该能够顺应技术的不断发展,不断的去掌握新技术.那么,对于WCF的掌握 ...
- [Sequence Alignment Methods] Cross-Recurrent Plot (CRP)
A recurrence plot (RP) is a straightforward way to visualize characteristics of similar system state ...
- 山东如意路嘉纳高级定制西装品牌惊艳亮相intertextile面料展 - 服装资讯中心 - 华衣网
山东如意路嘉纳高级定制西装品牌惊艳亮相intertextile面料展 - 服装资讯中心 - 华衣网 山东如意路嘉纳高级定制西装品牌惊艳亮相intertextile面料展
- IT项目经理应具备的十大软技能
现在,企业对IT部项目经理的要求越来越多.如果你认为IT项目成员只需要技术性能力,那可就错了. 据IT招聘公司调查发现,几年人们对项目管理软技能的兴趣明显浓厚起来.许多企业尽量避免把IT部门看成只是成 ...