【转载请注明】https://www.cnblogs.com/igoslly/p/9351564.html

具体的图示可查看 链接


代码一

/**
* 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) {
if(head==NULL || head->next ==NULL) return;
// two pointers split the list
// 快慢指针,快指针到尾时,慢指针在前list尾部
// example: 1->2->3->4->5 fast=5,slow=3, 1->2->3 & 4->5
// example: 1->2->3->4 fast=3,slow=2, 1->2 & 3->4
ListNode *slow = head, *fast = head;
while(fast->next !=NULL && fast->next->next !=NULL){
slow = slow ->next;
fast = fast->next->next;
}
ListNode *head1 = head;
// reverse the second list(with large numbers)
// 翻转第二个链表
ListNode *head2 = reverseList(slow->next);
slow->next = NULL;
while(head2!=NULL){ // list1 size >= list2 size
ListNode *next1 = head1->next;
ListNode *next2 = head2->next;
head1->next = head2;
head2->next = next1;
head1 = next1;
head2 = next2;
}
if(head1!=NULL){
head1->next = NULL;
}
}
// reverse list
ListNode *reverseList(ListNode *head){
if(head==NULL || head->next ==NULL) return head;
ListNode * new_head = NULL;
while(head!=NULL){
ListNode *pNext = head->next;
head->next = new_head;
new_head = head;
head = pNext;
}
return new_head;
}
};

/**
* 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) {
stack<ListNode* > nodestack;
int length=;
// 计算链表长度,结点压入stack
ListNode *temp = head;
while(temp){
length++;
nodestack.push(temp);
temp = temp->next;
}
// 栈弹出,连接链表
temp = head;
int cnt = length/;
while(cnt){
ListNode *head2 = nodestack.top();
nodestack.pop();
ListNode *headNext = temp->next;
temp->next =head2;
head2->next = headNext;
temp = headNext;
cnt--;
}
// 总数为odd,temp指向末尾元素
// 总数为even,temp会和前元素重复,此处删除
if(length%){
temp->next = NULL;
}else{
temp = NULL;
}
}
};

Leetcode0143--Reorder List 链表重排的更多相关文章

  1. 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...

    链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...

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

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

  3. 力扣—Reorder List(重排链表)python实现

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

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

  6. LeetCode(109):有序链表转换二叉搜索树

    Medium! 题目描述: 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: ...

  7. HashMap原理阅读

    前言 还是需要从头阅读下HashMap的源码.目标在于更好的理解HashMap的用法,学习更精炼的编码规范,以及应对面试. 它根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而 ...

  8. 深度剖析HashMap的数据存储实现原理(看完必懂篇)

    深度剖析HashMap的数据存储实现原理(看完必懂篇) 具体的原理分析可以参考一下两篇文章,有透彻的分析! 参考资料: 1. https://www.jianshu.com/p/17177c12f84 ...

  9. HashMap源码解析(JDK1.8)

    package java.util; import sun.misc.SharedSecrets; import java.io.IOException; import java.io.Invalid ...

随机推荐

  1. 九度oj 题目1059:abc

    题目1059:abc 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4510 解决:3546 题目描述: 设a.b.c均是0到9之间的数字,abc.bcc是两个三位数,且有:abc+bcc ...

  2. BZOJ 1853

    http://www.lydsy.com/JudgeOnline/problem.php?id=1853 岛娘在空间上发的题解就看了看果然被骗了.还以为是数位dp. 原来是容斥啊.好吧第一道正式的题目 ...

  3. vijos 1439 区间

    区间 背景 描述 给定n个闭区间 [ai,bi], i=1,2,...,n. 这些区间的和可以用两两不相交的闭区间的和来表示.你的任务是找到这样的区间数目最少的表示,且把它们按升序的方式写到输出文件中 ...

  4. W3School Memcached教程(安装/基本操作/高级操作/命令)

    来自W3School的Memcached教程,基本上涵盖了从安装到状态监控的教程. 不过最全的应该是官方提供在GitHub上的Wiki教程,一切的标准都来自官方,参考:https://github.c ...

  5. 工作总结 datatable 里的 数据 rows Columns

    json 格式数据 row  6行   每行 81 列   对应数据 col   81 列 每列代表字段

  6. 百度LBS云搜索时报错 &quot;filter:area is not filteable field, please set property in the cloud-storage

    {"status":2,"message":"filter:area is not filteable field, please set prope ...

  7. Android高仿UC浏览器和360手机卫士消息常驻栏(通知栏)

    之前网上看了下自己定义消息栏,通知栏,了解到了Notification这个控件.发现UC浏览器等都是这样的类型,今天写个demo实现下.如图: 当中每一个button都有不同的功能.代码例如以下: p ...

  8. UVa 489 Hangman Judge(字符串)

     Hangman Judge  In ``Hangman Judge,'' you are to write a program that judges a series of Hangman gam ...

  9. LeetCode 246. Strobogrammatic Number (可颠倒数字) $

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  10. Ubuntu12.04.3LTS安装Oracle Java 7

    今天在ubuntu 12.04 LTS 上安装Matlab时总是出错,显示拷贝jar包(cp "xx.jar")出现错误,在网上搜索了一下发现原来是没有安装java.自己打算安装o ...