[LeetCode]Reorder List

Windows Server 2016 下载 Windows Server 2016 下载 简介: Windows Server 2016是微软于2016年10月13日正式发布的最新服务器操作系统。基于Win10 1607(LTSB)内核所开发。 Windows Server 2016 Essentials edition(精华版) Windows Server 2016 Essentials版是专为小型企业而设计的。它对应于Win... 阅读详情

题目描述

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

对已有列表进行重排,使之按照L0→Ln→L1→Ln-1→L2→Ln-2→…的顺序排列。

解题思路

为了达到上述目标,我们采取以下策略:

  1. 获取列表长度len,计算得到尾部需要插入头部链表的长度为len/2;
  2. 得到头部链表的末尾节点,通过此节点获取尾部链表的头节点halfHead;
  3. 反转尾部链表节点,即以第2步获取的halfHead节点为头节点的链表;
  4. 将反转后的链表按序插入到头部链表的相邻节点中间;
  5. 结束,得到了重排后的链表。

代码

public  void reorderList(ListNode head) {
		ListNode node = head,halfHead = head,tempNode1,tempNode2;
		int len = 0, halfLen;
		if (node == null || node.next == null || node.next.next == null) {
			return;
		}

		while (node != null) {
			node = node.next;
			len++;
		}

		halfLen = len / 2;//得到需要重排节点的个数

		node = head;
		for (int i = 0;i < len - halfLen - 1; node = node.next, i++) {
		}
		halfHead = node.next;//得到尾部需插入头部节点的链表头
		node.next = null;
		
		node = head;
		ListNode reversehalfHead = reverseList(halfHead);//逆转链表
		
		//插入链表前部
		for(int i = 0;i < halfLen;i++){
			tempNode1 = node.next;
			tempNode2 = reversehalfHead.next;
			node.next = reversehalfHead;
			reversehalfHead.next = tempNode1;
			reversehalfHead = tempNode2;
			node = tempNode1;
		}	
	}

逆转链表的子函数:
public  ListNode reverseList(ListNode head) {
		ListNode node = head,lastNode = head,tempNode;
		if(node==null || node.next==null)
			return node;
		node = node.next;
		lastNode.next = null;
		while(node!=null){
			tempNode = node.next;
			node.next = lastNode;
			lastNode = node;
			node = tempNode;
		}
		return lastNode;
	}



北斗导航 | 基于matlab的卫星导航单点定位算法 单点定位(Single Point Positioning, SPP)通过接收机观测至少4颗卫星的伪距(含误差的距离测量值),解算接收机位置(经度、纬度、高度)及钟差。HDOP < 2 为高精度定位条件。 阅读详情

相关推荐

STM32 热敏电阻测量温度

1、硬件 STM32F103C8T6、热敏电阻传感器、OLED。 2、热敏电阻传感器的介绍

weixin_54721788的博客 1万+

LeetCode Reorder List

将单向链表L0→L1→…→Ln-1→Ln转化为L0→Ln→L1→Ln-1→L2→Ln-2→…的形式,也就是从头部取一个节点,从尾部取一个节点,直到将原链表转化成新的链表。

DRFish 2200

UE5 Metahuman与Blender手动绑定:ARKit面部动画实战对比

面部动画是角色动画领域的核心技术,其核心原理在于通过驱动面部骨骼或混合形状来模拟真实肌肉运动,从而赋予数字角色生命力。这项技术的价值在于极大地提升了虚拟角色的表现力与沉浸感,广泛应用于游戏开发、影视制作、虚拟现实和实时交互应用。在技术实现路径上,开发者常面临工业化集成方案与高度自定义方案的选择。本文聚焦于使用行业标准的ARKit 52个面部混合形状数据,深度对比了虚幻引擎Metahuman的工业化流程与Blender手动绑定方案在视觉保真度、工作流效率、性能开销和扩展性上的差异,为不同项目规模与技术需求的团

weixin_33901926的博客 455

Leetcode 143. Reorder List

Leetcode 143. Reorder List 问题描述:给定一个链表,要求把最后一位插入到第二位、倒数第二位插入到第三位… 链表问题很直接的思路就是断链、连链、暂存节点、暂存下个节点、快慢指针等操作。 继承前面142的思路,这里用快慢指针找到链表的中位数(index)位置,中位数后面的数字都是需要前插的,然后先进行链表的翻转,再进行合并得到最后的结果。 代码如下: class So...

Za七杂⑧ 173

从Excel到‘一张图’办案:手把手教你用AbutionGraph构建公安经侦知识图谱(含数据建模避坑指南)

本文详细介绍了如何利用AbutionGraph时序图数仓技术,将公安经侦领域的Excel、银行流水等碎片化数据转化为可交互的知识图谱。通过数据清洗、实体关系模型设计、时序数据处理等步骤,实现高效智能分析,提升办案效率。特别包含数据建模避坑指南和实战案例,助力公安金融案件侦破。

weixin_33702545的博客 347

YOLOv8改进策略【卷积层】| GnConv:一种通过门控卷积和递归设计来实现高效、可扩展、平移等变的高阶空间交互操作

HorNet: 利用递归门控卷积实现高效高阶空间交互GnConv(Recursive Gated Convolution,递归门控卷积)

Limiiiing的博客 328

Leetcode 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→… 原题链接 /** * Definition for singly-linked list. * struct ListNode { * int val; ...

UPUP&TOPTOP 182

CANoe之I/O功能采集电压

在进入ECU自动化测试中,有时会用到采集ECU电压,CANoe除了可以仿真、监听CAN、ETH、LIN等网络报文外,部分设备(如VN5650、VN1630等)上还有一个I/O口,这个I/O口就可以用来满足我们自动化测试中采集电压的需求。CANoe上的I/O功能用处还是特别大,比如网络管理测试、相关诊断都会用到。

测试领域技术分享 4855

[LeetCode] Reorder List

给定一个单链表 L:L0→L1→…→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 示例 1: 给定链表 1-&gt;2-&gt;3-&gt;4, 重新排列为 1-&gt;4-&gt;2-&gt;3. 这道链表重排序问题可以拆分为以下三个小问题: 使用快慢指针来找到链表的中点,并将链表从中...

by_qmr的博客 258

AUTOSAR AP——SM模块

对AUTOSAR AP中状态管理模块SM,从模块定义、总结等多方面进行了介绍。

weixin_43627682的博客 291

爬虫法律风险与合规边界

爬虫技术的法律边界正在快速收紧。从2017年上海晟品案开启爬虫入刑先河,到2025年Cloudflare区块链溯源技术将违规行为与企业主体永久绑定,合规已从"可选项"变为"必选项"。技术团队需要建立三层防御体系法律合规层:法律适用判定、授权管理、合同审查、隐私政策;技术控制层:访问控制、数据加密、匿名化、审计日志、监控告警;组织管理层:DPO任命、培训体系、应急响应、合规审计、绩效考核。

从事移动开发八年,csdn博客专家,阿里云特邀专家,华为云云享专家,高级网络信息安全工程师,大数据工程师(高级),python技术开发(高级),HarmonyOS 应用开发者高级认证,现在某学校任教。 1173

[Leetcode Week6]Reorder List

Reorder List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/reorder-list/description/ Description 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...

weixin_30500663的博客 124

保姆级教程!Python爬虫爬取知乎回答,保存为Markdown文件

本教程实现了知乎公开回答的完整爬取与Markdown保存,核心流程为「请求页面→解析作者/时间/正文/图片→下载图片→保存MD文件」,步骤清晰,代码可直接复用关键特性:支持合并/单文件两种保存模式、自动下载图片并引用、中文乱码防护、非法文件名过滤,满足日常整理知乎回答的需求新手友好:无需复杂配置,仅需修改知乎问题URL即可运行,全程无坑,5分钟内可看到运行结果灵活扩展:支持进阶优化(登录、分页、格式完善),可根据个人需求调整功能。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1236

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 t

Peep Hole 1127

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

【LeetCode】143. Reorder List 解题报告(Python)

【LeetCode】143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/reorder-list/descri...

负雪明烛 2901

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

LeetCode 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 modify the values in the list’s nodes, only nodes itself may be changed. Example 1: Given 1->2->3->4, reorder it to 1-

旺崽的博客 2449

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

code to success 623

【LeetCode】 143. Reorder List 重排链表(Medium)(JAVA)每日一题

【LeetCode】 143. Reorder List 重排链表(Medium)(JAVA) 题目地址: https://leetcode.com/problems/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 modify the values in the list’s nodes, only nodes itse

qq_16927853的博客 209

LeetCode题解之Reorder List

1、题目描述 2、题目分析 首先将链表分为两段,然后将后面的一段反转,再合并两个链表。 3、代码 1 void reorderList(ListNode* head) { 2 if (head == nullptr || head->next == nullptr || head->next->next == nullptr) 3 ...

weixin_30270561的博客 161

Leetcode 143. Reorder List (Medium) (cpp)

Leetcode 143. Reorder List (Medium) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 419

[LeetCode][143] Reorder List

先遍历一遍知道有多长 然后取到中间把整个链表断开 然后把后半部分反转 并且一一merget到前部份 /* * @lc app=leetcode id=143 lang=cpp * * [143] Reorder List * * https://leetcode.com/problems/reorder-list/description/ * * algorithms * Med...

OCEANtroye的博客 328

leetcode143. 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 modify the values in the list’s nodes, only nodes itself may be changed. Example 1: Given 1->2...

中华小当家的博客 206

Reorder List|leetcode题解

此题思路比较简单,分为三步: 1.利用快慢指针,将链表从中间断开。 2.翻转右半部分链表 3.将两个链表合并 首先,是我提交的第一个版本,耗时880mm,非常的慢 ListNode *reverseList(ListNode *head){ if(NULL==head)return NULL; if(NULL==head->next)return head;

xueluo0000的专栏 419

leetcode reorder-list(Java)

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

聚沙成塔 316

[LeetCode 143] Reorder List Solution

Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder

程序人生 1288
上一篇: [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
下一篇: [LeetCode]Reverse Words in a String
彩虹之西
博客等级 码龄16年 18粉丝 133原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值