当前位置: 首页 > news >正文

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 this in-place without altering the nodes' values.


For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.


本题算是链表中很有特点的一道题目。对于链表1->2->3->4->5->6 ,要变成1->6->2->5->3->4,
即第i个节点指向倒数第i个节点,而倒数第i个节点,指向第i+1个节点。



解法一:
使用前后两个指针p和q。
具体实现步骤在注释中有详细说明,在此不再赘述。




由于时间复杂度为O(N^2),不够高效导致会超时。无法通过OJ的测试数据。


实现代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void ReorderList(ListNode head) 
    {
        if(head == null || head.next == null || head.next.next == null){
    		return;
    	}
    	
    	var p = head;
    	var q = head;
    	while(q.next.next != null){
    		while(p.next.next != null){
    			p = p.next;
    		}
    		
    		// point head to last
    		var t = q.next;
    		q.next = p.next;
    		// point last to 2nd and set the second last to null
    		p.next.next = t;
    		
    		// point 2nd last to null
    		p.next = null;
    
    		// reset p and q
    		p = t;
    		q = t;
    		if(q.next == null){
    			break;
    		}
    	}
    }
}





解法二:


本实现参考了连接:
http://www.acmerblog.com/reorder-list-leetcode-6088.html




1.使用slow和fast指针将链表分为两部分,part1和part2 ,假设链表为1->2->3->4->5->6->7->8, part1 = {1->2->3->4} , part2= {5->6->7->8}
2.然后对part2逆置,即8->7->6->5
3.然后分别将part1[0]->part2[0], part2[0]->part1[1], part1[1]->part2[1]...
即,对于i < len - 1
part1[i] -> part2[i]
part2[i] -> part1[i+1]
i++


实现代码:





/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void ReorderList(ListNode head) 
    {
        if(head == null || head.next == null || head.next.next == null){
    		return;
    	}
    	
    	var slow = head;
    	var fast = head;
    	while(fast.next != null && fast.next.next != null)
    	{
    		slow = slow.next;
    		fast = fast.next.next;
    	}
    	
    	var mid = slow.next;
    	var last = mid;
    	ListNode pre = null;
    	while(last != null){
    		ListNode next = last.next;
    		last.next = pre;
    		pre = last;
    		last = next;
    	}
    	slow.next = null;
    	
    	while(head != null && pre != null)
    	{
    		var next1 = head.next;
    		head.next = pre;
    		pre = pre.next;
    		head.next.next = next1;
    		head = next1;
    	}
    }
}


相关文章:

  • 最近
  • LeetCode -- Search a 2D Matrix II
  • [IE编程] 打开/关闭IE8的光标浏览模式(Caret Browsing)
  • LeetCode -- 3Sum Closest
  • 使用反射将业务对象绑定到 ASP.NET 窗体控件
  • LeetCode -- 4Sum
  • LeetCode -- Binary Tree Level Order Traversal II
  • (ZT)一个美国文科博士的YardLife
  • LeetCode -- Clone Graph
  • Oracle 中的nvl() 函数 相当于Sql Server 的 isnull()
  • LeetCode -- Combinations
  • [IE编程] WebBrowser控件中设置页面的缩放
  • LeetCode -- Find the Duplicate Number
  • LeetCode -- Group Anagrams
  • LeetCode -- Kth Largest Element in an Array
  • 《Java编程思想》读书笔记-对象导论
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • 【前端学习】-粗谈选择器
  • flask接收请求并推入栈
  • Java Agent 学习笔记
  • js如何打印object对象
  • leetcode讲解--894. All Possible Full Binary Trees
  • passportjs 源码分析
  • Python3爬取英雄联盟英雄皮肤大图
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 学习笔记:对象,原型和继承(1)
  • 再次简单明了总结flex布局,一看就懂...
  • 最近的计划
  • gunicorn工作原理
  • ​Linux·i2c驱动架构​
  • #FPGA(基础知识)
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • #Z0458. 树的中心2
  • #快捷键# 大学四年我常用的软件快捷键大全,教你成为电脑高手!!
  • (14)Hive调优——合并小文件
  • (Forward) Music Player: From UI Proposal to Code
  • (附源码)ssm本科教学合格评估管理系统 毕业设计 180916
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
  • (转)Sublime Text3配置Lua运行环境
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .net Application的目录
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .Net多线程总结
  • .net项目IIS、VS 附加进程调试
  • .pings勒索病毒的威胁:如何应对.pings勒索病毒的突袭?
  • [ 云计算 | AWS 实践 ] Java 如何重命名 Amazon S3 中的文件和文件夹
  • [2017][note]基于空间交叉相位调制的两个连续波在few layer铋Bi中的全光switch——
  • [20171106]配置客户端连接注意.txt
  • [Android 13]Input系列--获取触摸窗口
  • [android学习笔记]学习jni编程
  • [Angular 基础] - 指令(directives)
  • [BZOJ 3531][Sdoi2014]旅行(树链剖分+线段树)
  • [C#]OpenCvSharp使用帧差法或者三帧差法检测移动物体