143 Reorder List 重排链表
给定一个单链表L:L0→L1→…→Ln-1→Ln,
重新排列后为: L0→Ln→L1→Ln-1→L2→Ln-2→…
必须在不改变节点的值的情况下进行原地操作。
例如,
给定链表 {1,2,3,4},按要求重排后为 {1,4,2,3}。
详见:https://leetcode.com/problems/reorder-list/description/
Java实现:
1、使用快慢指针来找到链表的中点,并将链表从中点处断开,形成两个独立的链表;
2、将第二个链翻转;
3、将第二个链表的元素间隔地插入第一个链表中。
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- class Solution {
- public void reorderList(ListNode head) {
- if(head==null||head.next==null){
- return;
- }
- ListNode slow=head;
- ListNode fast=head;
- while(fast!=null&&fast.next!=null){
- slow=slow.next;
- fast=fast.next.next;
- }
- fast=slow.next;
- slow.next=null;
- ListNode pre=null;
- ListNode next=null;
- while(fast!=null){
- next=fast.next;
- fast.next=pre;
- pre=fast;
- fast=next;
- }
- slow=head;
- fast=pre;
- ListNode post1=null;
- ListNode post2=null;
- while(slow!=null&&fast!=null){
- post1=slow.next;
- post2=fast.next;
- slow.next=fast;
- fast.next=post1;
- slow=post1;
- fast=post2;
- }
- }
- }
参考:https://www.cnblogs.com/grandyang/p/4254860.html
143 Reorder List 重排链表的更多相关文章
- [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 may not mod ...
- [Leetcode] Reorder list 重排链表
Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...
- Leetcode143. Reorder List重排链表
给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例 1: ...
- Java实现 LeetCode 143 重排链表
143. 重排链表 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节 ...
- 【Warrior刷题笔记】143.重排链表 【线性化 || 双指针+翻转链表+链表合并】详细注释
题目一 力扣143.重排链表 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reorder-list/ 1.描述 给定一个单链表L的头节点he ...
- Leetcode 143.重排链表
重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 143. Reorder List - LeetCode
Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...
随机推荐
- VC实现趋势图绘制
本文参考pudn上一个完整工程,在pudn搜索“50815867CurveDrawing”即可找到源代码. 上图是使用VS2010重写了该软件后的效果图,下面再贴出关键代码: // Plot.cp ...
- devm_regmap_init_i2c【转】
本文转载自:http://blog.csdn.net/u011975319/article/details/52128845 本文有此处转载http://blog.csdn.net/luckywang ...
- POJ3685 Matrix —— 二分
题目链接:http://poj.org/problem?id=3685 Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissio ...
- uptime命令
uptime命令能够打印系统总共运行了多长时间和系统的平均负载.uptime命令可以显示的信息显示依次为:现在时间.系统已经运行了多长时间.目前有多少登陆用户.系统在过去的1分钟.5分钟和15分钟内的 ...
- hdu 超级楼梯 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 哦--对了,这些题读者可以直接忽略,我只是想练习一下自己薄弱的地方...... 题目意思我就不说 ...
- Oracle :多实例切换
Connecting to 10.1.4.21:22...Connection established.To escape to local shell, press 'Ctrl+Alt+]'. La ...
- 原:maven+springMVC+mybatis+junit详细搭建过程
阅读目录 1. 工程目录结构整理清楚 2. 引入依赖包 3. 配置数据库连接属性 4. 配置spring配置文件 5. java代码编写(model,dao,service层代码) 6. m ...
- limit的用法
limit子句可以用于强制select语句返回指定的记录数.limit接受一个或两个数字参数,参数必须是整数常量.如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最 ...
- spring+mybatis 多数据源整合--temp
<!-- 数据源配置 --> <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSour ...
- 当你触摸并按住触摸目标时候,禁止系统默认菜单-webkit-touch-call
当你触摸并按住触摸目标时候,禁止或显示系统默认菜单. -webkit-touch-callout 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS ...