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. 原创+部分引用啦:C# Winform界面中的分隔线问题

    C sharp 中Winform中的控件很多,一个小小的问题居然会绕上一个小弯子,做界面的时候, 你需要在界面上弄一条分隔线,把相关的功能分隔开来,结果原来在其它 IDE编辑器里很容易实现的这个功能, ...

  2. (转)Java程序利用main函数中args参数实现参数的传递

    Java程序利用main函数中args参数实现参数的传递 1.运行Java程序的同时,可以通过输入参数给main函数中的接收参数数组args[],供程序内部使用!即当你在Java命令行后面带上参数,J ...

  3. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

  4. Onthink_项目后总结

    ---------------------------------------写代码不孤独__小小代(http://www.cnblogs.com/xiaoxiaodai/) 经过一段时间的沉寂,项目 ...

  5. Stripies(POJ 1862 贪心)

    Stripies Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14151   Accepted: 6628 Descrip ...

  6. 三维地图(BFS)

    亡命逃窜 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 从前有个叫hck的骑士,为了救我们美丽的公主,潜入魔王的老巢,够英雄吧.不过英雄不是这么好当的.这个可怜的娃 ...

  7. 学习 ExtJS 4 面板与布局

    原文 http://www.cnblogs.com/codealone/archive/2013/06/04/3091325.html 面板Panel Ext.panel.Panel拓展自Ext.co ...

  8. mysql 保留的关键字

    mysql> select precision from Product; ERROR 1064 (42000): You have an error in your SQL syntax; c ...

  9. excel内容转成xml

    简单记录下如何将excel中的一个表格内容转成xml格式的文件. excel菜单栏中的"开发工具"下有专门处理xml的模块,如下图. 如果你的excel中看不到"开发工具 ...

  10. UESTC_树上战争 CDOJ 32

    给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜. Input 输入包含多组数据 每组第 ...