用快慢双指针,可以使慢指针到达中间的时候快指针到达最后一个元素(奇数),或者倒数第二个元素(偶数)。慢指针后面的元素是后半个链表,把后半个链表进行reverse,然后再插在原来的链表中就可以了

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
ListNode* reverse(ListNode* head)
{
ListNode* pre;
ListNode* cur;
cur = head;
pre = NULL;
while (cur!= NULL)
{
ListNode* next;
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
public:
void reorderList(ListNode* head) {
if (head==NULL||head->next == NULL || head->next->next == NULL)
return;
ListNode* fast;
ListNode* slow;
fast = head;
slow = head;
while (fast->next&&fast->next->next)//出来之后slow的下一个就是后半段.
{
fast = fast->next->next;
slow = slow->next;
}
ListNode*q;
q = reverse(slow->next);
slow->next = NULL;
ListNode*p;
p = head;
while (p&&q)
{
ListNode* temp;
temp = new ListNode(q->val);
temp->next=p->next;
p->next = temp;
p = temp->next;
q = q->next;
}
return;
}
};

  

leetcode143. Reorder List的更多相关文章

  1. Leetcode143. Reorder List重排链表

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

  2. [Swift]LeetCode143. 重排链表 | 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 may not mod ...

  3. LeetCode143: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 d ...

  4. [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 must do th ...

  5. 【leetcode】Reorder List (middle)

    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 ...

  6. 13. Reorder List

    Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Y ...

  7. String reorder

    本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions) Description For th ...

  8. 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 ...

  9. 62. 链表重排[Reorder List]

    [本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...

随机推荐

  1. 【Android】PreferenceActivity 详解

    PreferenceActivity是专业的设置界面,只要给它指定一个配置好的xml,它就能自动根据操作更改程序Preference的相应值. 首先要用一个xml文件来配置一个设置界面,也就是我们说的 ...

  2. python 10大算法之一 LinearRegression 笔记

    简单的线性回归预测房价 #!/usr/bin/env python # encoding: utf-8 """ @version: @author: --*--. @fi ...

  3. 这篇文章主要介绍了Citrix XenServer 6.1 安装图解教程

    本次为使用VirtualBox虚拟机过安装测试机过程,我们在使用Vm(无论是Vbox还是VMware等)我们的CPU都必须可支持Intel-V或AMD-V,并且在VM软件设置和BIOS设置开启虚拟化支 ...

  4. 移动端1px边框实现

    问题描述:移动端iPhone上的1px边框看起来像2px那么粗.问题分析:不同的手机有不同的像素密度,在window对象中有一个devicePixelRatio属性,它可以反应设备的像素与css中的像 ...

  5. Mac解决某些命令失效问题

    PS:今天安装groovy,需要设置环境变量,vim -/.bash_profile,然后立马执行了source -/.bash_profile.最后,发现ls,more,vim等常用命令失效了.立马 ...

  6. python-ironicclient使用

    使用cli from ironicclient import client kwargs = {'os_username': 'ironic', 'os_password': 'IRONIC_PASS ...

  7. defer 内追踪变量变化

    遇到一个需求,需要追踪变量的最终情况.defer比较合适,但是写了变量和指针都无效,于是试了试: 变量,变量地址,指针的使用情况 func TestDefer(t *testing.T) { a := ...

  8. 转 MYSQL InnoDB Record, Gap, and Next-Key Locks

    http://dev.mysql.com/doc/refman/5.0/en/innodb-record-level-locks.html InnoDB has several types of re ...

  9. Hangover POJ - 1003

    How far can you make a stack of cards overhang a table? If you have one card, you can create a maxim ...

  10. [Codeforces Round #438][Codeforces 868C. Qualification Rounds]

    题目链接:868C - Qualification Rounds 题目大意:有\(n\)个题目,\(k\)个人,每个人可能做过这\(n\)个题里的若干道,出题方要在这\(n\)个题目里选若干个出来作为 ...