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

【LeetCode】链表反转实现与测试

概述

本文介绍了一种使用 ArrayList 和 Collections.reverse() 方法来实现链表反转的解决方案,虽然这种方法在空间复杂度上不是最优的,但代码简洁易懂。

实现思路

  1. 遍历原链表,将所有节点的值收集到 ArrayList 中
  2. 使用 Collections.reverse() 方法反转 ArrayList
  3. 根据反转后的值列表重新构建链表

源码实现

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class ReverseNode {public ListNode reverseList(ListNode head) {if (head == null || head.next == null) {return head;}// 定义一个收集值的数组List<Integer> valList = new ArrayList<>();while (head.next != null) {valList.add(head.val);// 移动链表head = head.next;}// 加上最后一个节点的值valList.add(head.val);// 反转收集值Collections.reverse(valList);ListNode cursorNode = new ListNode();for (int index = 0; index < valList.size(); index++) {Integer val = valList.get(index);if (index == 0) {// 初始化头节点head = new ListNode(val);cursorNode = head;continue;}cursorNode.next = new ListNode(val);// 移动游标节点cursorNode = cursorNode.next;}return head;}static class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}}public static void main(String[] args) {ReverseNode reverseNode = new ReverseNode();// 测试用例1:正常链表 1->2->3->4->5ListNode head1 = new ListNode(1);head1.next = new ListNode(2);head1.next.next = new ListNode(3);head1.next.next.next = new ListNode(4);head1.next.next.next.next = new ListNode(5);System.out.println("原链表1:");printList(head1);ListNode reversed1 = reverseNode.reverseList(head1);System.out.println("反转后:");printList(reversed1);System.out.println();// 测试用例2:单节点链表ListNode head2 = new ListNode(42);System.out.println("原链表2:");printList(head2);ListNode reversed2 = reverseNode.reverseList(head2);System.out.println("反转后:");printList(reversed2);System.out.println();// 测试用例3:空链表ListNode head3 = null;System.out.println("原链表3:");printList(head3);ListNode reversed3 = reverseNode.reverseList(head3);System.out.println("反转后:");printList(reversed3);System.out.println();// 测试用例4:两个节点的链表ListNode head4 = new ListNode(10);head4.next = new ListNode(20);System.out.println("原链表4:");printList(head4);ListNode reversed4 = reverseNode.reverseList(head4);System.out.println("反转后:");printList(reversed4);}private static void printList(ListNode head) {if (head == null) {System.out.println("null");return;}ListNode current = head;while (current != null) {System.out.print(current.val);if (current.next != null) {System.out.print(" -> ");}current = current.next;}System.out.println();}
}
http://www.dtcms.com/a/306365.html

相关文章:

  • ansible巡检脚本
  • 2025年7月28日–7月29日 · AI 今日头条
  • 串口接收数据包(协议带帧头帧尾)的编程实现方法:1、数据包格式定义结构体2、使用队列进行数据接收、校验解包
  • centos7 aarch64上安装PostgreSQL14.3
  • 如何在生成式引擎优化(GEO)中取得成功
  • Java:高频面试知识分享1
  • 比特币挖矿的能源消耗和环保问题
  • 【Linux】重生之从零开始学习运维之备份恢复
  • CONTRASTIVE-KAN:一种用于稀缺标记数据的网络安全半监督入侵检测框架
  • Apache Kafka核心组件详解
  • click和touch事件触发顺序 糊里糊涂解决的奇怪bug
  • 开源 Arkts 鸿蒙应用 开发(十二)传感器的使用
  • WiFi连接简单流程
  • Linux命令---服务管理类命令
  • EPOLL 的用法
  • 报考民航安检员证需要具备哪些条件?
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | VerifyAccountUi(验证码组件)
  • git本地仓库,工作区和暂存区的知识
  • SpringBoot之多环境配置全解析
  • 实现implements InitializingBean, DisposableBean 有什么用
  • AI 代码助手在大前端项目中的协作开发模式探索
  • 关于tresos Studio(EB)的MCAL配置之MCU
  • 商标注册后可以随意更改字体和颜色吗!
  • Vue3 中 toValue 与 unref 深度解析:异同、场景与最佳实践
  • 单片机学习笔记.AD/DA(略含有SPI,用的是普中开发板上的XPT2046芯片)
  • 力扣209:长度最小的子数组
  • 锁定中科院1区TOP!融合LSTM与Attention做时间序列预测 !
  • Metering Solution for Solar + Storage光伏+储能计量解决方案 UL 2735 Certification功率表能源监测电表
  • 电池自动生产线:科技赋能下的高效制造新范式
  • ‌CASE WHEN THEN ELSE END‌