class Node {
private int data;
private Node next;
}
public static Node reverseLinkedList(Node head){
if (head == null || head.next == null){
return head;
}
Node first = head;
Node cur = head.next;
Node next;
while(cur != null){
next = cur.getNext();
cur.setNext(first);
first = cur;
cur = next;
}
head.setNext(null);
head = first;
return head;
}java反转单链表
最新推荐文章于 2024-10-20 23:18:17 发布
本文详细介绍了链表数据结构中的一种经典操作——链表反转,并提供了实现这一功能的Java代码示例。通过迭代的方式,文章逐步展示了如何将一个单向链表的节点顺序进行反转,这对于理解链表的工作原理及其基本操作十分关键。
328

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



