package com.example.leetcode;
public class ReverseList {
public ListNode reverse(ListNode head){
if (head==null){
return null;
}
ListNode pre=head;
ListNode current=head.next;
//最初的时候要指向null
pre.next=null;
while (current!=null){
//先判断下个节点有没有值
ListNode next =current.next;
//将当前节点指向前一个节点
current.next=pre;
//向后移动
pre=current;
//向后移动
current=next;
}
return pre;
}
}
Java 链表反转
最新推荐文章于 2025-12-19 15:39:34 发布
该博客详细介绍了如何使用Java编程语言实现链表的反转功能。通过迭代方式,逐个节点调整指针方向,最终完成链表的反转操作。这个算法在数据结构和算法领域具有重要意义,对于理解和操作链表数据结构十分有用。
1703

被折叠的 条评论
为什么被折叠?



