Leetcode0143--Reorder List 链表重排
【转载请注明】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 链表重排的更多相关文章
- 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...
链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...
- 62. 链表重排[Reorder List]
[本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...
- 力扣—Reorder List(重排链表)python实现
题目描述: 中文: 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点 ...
- [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 ...
- 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(109):有序链表转换二叉搜索树
Medium! 题目描述: 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: ...
- HashMap原理阅读
前言 还是需要从头阅读下HashMap的源码.目标在于更好的理解HashMap的用法,学习更精炼的编码规范,以及应对面试. 它根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而 ...
- 深度剖析HashMap的数据存储实现原理(看完必懂篇)
深度剖析HashMap的数据存储实现原理(看完必懂篇) 具体的原理分析可以参考一下两篇文章,有透彻的分析! 参考资料: 1. https://www.jianshu.com/p/17177c12f84 ...
- HashMap源码解析(JDK1.8)
package java.util; import sun.misc.SharedSecrets; import java.io.IOException; import java.io.Invalid ...
随机推荐
- 中文命名之Hibernate 5演示 - 使用注解(annotation)而非xml定义映射
前文中文编程:中文命名之Hibernate 4+MySQL演示最后留下了个Hibernate 5之后出现的问题, 于是在Hibernate社区提交了报告: Seemingly regression s ...
- Spark之安装和使用
Scala安装 Spark使用Scala开发,安装的Scala版本必须和Spark使用的版本一致,否则使用Scala编写的Spark任务会有兼容性问题 可以到Spark官网查看确定Scala版本,或者 ...
- fastjson将java list转为json字符串
1.直接用fastjson的静态方法string JSON.toJSONString(list)方法就行,JSON.toJSONString(list)将java list转为json字符串. 2.t ...
- [JavaEE] JTA, Java Transaction API, Repository for DB opreations
Mainly two things: 1. For all the creating and deleting opreations for the DB, we want to use 'REQUI ...
- Mac OS忘记password怎么办?无光盘破解Mac OS的管理员password
mac系统10.8.5升级10.10 出现故障,重新启动系统无法登陆,降级系统10.9 后更新10.9.3 finder无法打开,root权限没了,又再一次覆盖安装10.9(为了保护原系统文件.所以覆 ...
- HDFS学习笔记(2)hdfs_shell & JavaAPI
FileSystem shell指令 官方文档: HDFS Commands Reference appendToFile cat checksum chgrp chmod chown copyFro ...
- ACdream 1125(ACfun-字典序)
A - ACfun Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...
- Magento 模块开发之DispatchEvent
在这一章节中.我们来了解 Magento 中的事件分发机制 Mage::dispatchEvent() 在创建自己的模块时, Event 事件的分发将会变成十分实用且有效 以个人的经验. 事件的分发使 ...
- 在项目开发中使用Git版本号控制工具以提高效率
安装Git(linux centos平台) 源代码方式安装 1.装依赖 $ yum install curl-devel expat-devel gettext-devel openssl-devel ...
- Server Tomcat v8.0 Server at localhost failed to start.
怎么办? 查资料的话别人会告诉你须要删除一个东西.这是一种方法.可是你的错误并不是通过这种方法能够解决. 比方像我 <url-pattern>login</url-pattern&g ...