下面是其中一种写法,也可以有不同的写法,比如递归等。供参考。
static class Node{
private int data;
private Node next;
Node(int d){
data = d;
next = null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public static void reverse(Node head)
{
if(null == head || null == head.getNext()){
return;
}
Node prev=null;
Node pcur = head.getNext();
Node next;
while(pcur!=null){
if(pcur.getNext()==null){
pcur.setNext(prev);
break;
}
next=pcur.getNext();
pcur.setNext(prev);
prev=pcur;
pcur=next;
}
head.setNext(pcur);
Node tmp = head.getNext();
while(tmp!= null){
System.out.print(tmp.getData()+" ");
tmp=tmp.getNext();
}
}
来源:https://blog.csdn.net/yjn1995/article/details/99689364