class Demo
{
public Ndoe reverse(Node head)
{
Node pre = null;
Node next = null;
while(head!=null)
{
//next 中间变量保存head.next的数据
next = head.next;
head.next = pre;
pre =head;
//完成head和pre的交换
head= next;
}
return pre;
}
}
class Node
{
String data;
Node next;
public Node(Node next,String data)
{
this.data = data;
this.next = next;
}
}
《剑指offer》反转链表
最新推荐文章于 2022-06-23 19:15:08 发布