otal Accepted: 54991 Total Submissions: 252600 Difficulty: Medium

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes' values.

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:
ListNode* reverseList(ListNode* head)
{
ListNode* cur = head;
ListNode* newHead = NULL;
ListNode* next = NULL;
while(cur){
next = cur->next;
cur->next = newHead;
newHead = cur;
cur = next;
}
return newHead;
} void reorderList(ListNode* head)
{
if(head==NULL || head->next==NULL){
return;
}
ListNode* slow = head;
ListNode* fast = head;
ListNode* pre = NULL;
while(fast){
pre = slow;
slow = slow->next;
fast->next ? fast = fast->next->next : fast = NULL;
}
pre->next = NULL;
ListNode* head2 = reverseList(slow); ListNode* cur = head;
ListNode* cur_next = NULL;
ListNode* cur2 = head2;
ListNode* cur2_next = NULL;
while(cur2){
cur_next = cur->next;
cur2_next = cur2->next; cur->next = cur2;
cur2->next = cur_next; cur = cur_next;
cur2 = cur2_next;
}
}
};

[Linked List]Reorder List的更多相关文章

  1. [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. * str ...

  2. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

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

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

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

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

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

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

  8. Java for LeetCode 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 th ...

  9. Reorder List [LeetCode]

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

随机推荐

  1. TransactionScrope

    测这个东西其实是由生产环境数据库报错,ORA-14450错误. 测试结果是: 1)使用transactionscrope时,数据库连接打开需在scrope内打开: 2)TransactionScope ...

  2. php中mail()改用msmtp发送邮件

    php中的mail()函数默认调用的是sendmail,这次我将其换成了轻量级的msmtp.在次过程中涉及到:修改配置文件php.ini,创建配置文件msmtprc或(.msmtprc)并修改其权限. ...

  3. 用GDAL/OGR去读shapefile

    一.读shapefile 1.首先,用Arcgis创建所要读的shp文件.打开ArcCatalog,右键NEW->Shapefile,名称Name:point ,要素类型(Feature Typ ...

  4. SQL SERVER中如何格式化日期

    1. SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM)    -- Oct 2 2008 11:01AM  ...

  5. C#解析JSON数据

    本篇文章主要介绍C#对Json数据的读取. 主要操作过程是: 发送Http请求获取Json数据 把获取的Json数据转换成C#中的类 下面我们以12306火车票余票的数据为例进行切入. 首先来看一下h ...

  6. java使用dom4j和XPath解析XML与.net 操作XML小结

    最近研究java的dom4j包,使用 dom4j包来操作了xml 文件 包括三个文件:studentInfo.xml(待解析的xml文件), Dom4jReadExmple.java(解析的主要类), ...

  7. [Head First Python]4. pickle.dump pickle.load

    sketch.py #--*-- coding:utf-8 --*-- import pickle import nester man = [] other = [] try: data = open ...

  8. shell练习--批量创建账号

    #!/bin/bash #By spinestars #-- #cksum5位数获取方法,可能有重复 #pd="user`head -200 /dev/urandom | cksum | h ...

  9. Nightmare(BFS)

    #include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...

  10. jQuery.data的是jQuery的数据缓存系统

    jQuery.Data源码 jQuery.data的是jQuery的数据缓存系统 jQuery.data的是jQuery的数据缓存系统.它的主要作用就是为普通对象或者DOM元素添加数据. 1 内部存储 ...