当前位置: 首页 > 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.lryc.cn/news/605212.html

相关文章:

  • (补题)小塔的饭
  • sqLite 数据库 (3):以编程方式使用 sqLite,4 个函数,以及 sqLite 移植,合并编译
  • linux 执行sh脚本,提示$‘\r‘: command not found
  • C语言:函数指针、二级指针、常量指针常量、野指针
  • 【Kubernetes 指南】基础入门——Kubernetes 201(二)
  • Vite 模块动态导入之Glob导入
  • Cursor MCP搭建入门
  • 力扣热题100---------35.搜索插入为位置
  • jQuery UI Tabs切换功能实例
  • Python在自动化与运维领域的核心角色:工具化、平台化与智能化
  • Jaeger理论、实战、问题记录
  • TikTok 视频审核模型:用逻辑回归找出特殊类型的视频
  • Elasticsearch 文档操作管理:从增删改查到批量操作与数据类型
  • 硬性巩膜镜市场报告:深度解析行业现状与未来趋势
  • Java项目:基于SSM框架实现的济南旅游网站管理系统【ssm+B/S架构+源码+数据库+毕业论文+远程部署】
  • 同一雷达不同样式的pdw数据
  • FFmpeg:因码流采集与封装不同步导致录制出来的MP4文件会出现黑屏、绿屏的问题
  • 达梦数据库(DM Database)角色管理详解|了解DM预定义的各种角色,掌握角色创建、角色的分配和回收
  • 实现了加载 正向 碰撞 雅可比 仿真
  • 第十九周-文档数据库MongoDB、消息队列和微服务
  • I Built an Offline-Capable App by Myself: React Native Frontend, C# Backend
  • WebSocket 简介与在 Vue 中的使用指南
  • Python正则表达式精准匹配独立单词技巧
  • ACL 2025 第二弹:维也纳风情舞会点燃学术之夜
  • 论文阅读:《多目标和多目标优化的回顾与评估:方法和算法》
  • Three.js + AI:结合 Stable Diffusion 生成纹理贴图
  • 如何在 Ubuntu 24.04 或 22.04 LTS 上安装 Deepin 终端
  • 微软OpenAI展开深入谈判
  • SpringCloud -- MQ高级
  • Tdesign-React 模板面包屑如何放到 Header头部