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

相关文章:

  • 比特币挖矿的能源消耗和环保问题
  • 关于“LoggerFactory is not a Logback LoggerContext but Logback is on ......“的解决方案
  • C++代码题部分(1)
  • LLM 模型部署难题的技术突破:从轻量化到分布式推理的全栈解决方案
  • AutoSAR(MCAL) --- ADC
  • Linux虚拟内存
  • 【C#】DevExpress.XtraEditors.MemoEdit memoEditLog控件讲解
  • AI服务器中,EEPROM有哪些部件使用,需要存储哪些信息?
  • Syzkaller实战教程2:运行环境配置+实例运行
  • 在Trae中使用MoonBit月兔
  • Android调用python库和方法的实现
  • 三十四、【Linux常用工具】rsync+inotify实时同步演示
  • GitHub使用小记——本地推送、外部拉取和分支重命名
  • Camera相机人脸识别系列专题分析之十九:MTK ISP6S平台FDNode传递三方FFD到APP流程解析
  • git本地仓库,工作区和暂存区的知识
  • llama factory本地部署常见问题
  • 用Python+MySQL实战解锁企业财务数据分析
  • 会吸的簸箕专利拆解:迷你真空组件的吸力控制与吸入口设计原理
  • Redis学习------缓存穿透
  • 数据建模怎么落地?从概念、逻辑到物理模型,一文讲请!
  • Prometheus-2--什么是Exporter是什么?
  • Spring boot 打包成docker image 镜像
  • 数据结构第3问:什么是线性表?
  • (RedmiBook)上禁用触摸板或自带键盘
  • 4.方法的使用
  • OmniParser:提升工作效率的视觉界面解析工具
  • 【深度学习新浪潮】3D城市建筑多样化生产的研发进展调研
  • Kafka 单机多 Broker 实例集群搭建 | 详情
  • 【机器学习】机器学习新手入门概述
  • 如何将DICOM文件制作成在线云胶片