public class Node {
private Node next;
private String value;
public Node(String value){
this.value=value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
/**
* 利用迭代循环到链表最后一个元素,然后利用nextNode.setNext(head)把最后一个元素变为
* 第一个元素。
*
* @param head
*/
public static void reverse(Node head){
if(head!=null){
Node nextNode=head.getNext();
if(nextNode!=null){
reverse(nextNode);
nextNode.setNext(head);
}
}
}
}
用JAVA实现单链表的翻转
最新推荐文章于 2022-09-10 16:00:00 发布