给定一个单链表,将其反转的方法一般有两种,第一种为常规遍历法,通过设置两个空节点来传值。
private static void reverse(Node node) {
// TODO Auto-generated method stub
Node pre= null;
Node next = null;
while(node!=null){
next = node.next;
node.next = pre;
pre = node;
node=next;
}
}
第二种为递归法,属于系统压栈
private static Node reverse1(Node n1) {
// TODO Auto-generated method stub
if(n1==null ||n1.next==null) return n1;
Node temp=n1.next;
Node newhead = reverse1(n1.next);
temp.next=n1;
n1.next=null;
return n1;
}
https://blog.csdn.net/qq_29660957/article/details/98068613
https://www.cnblogs.com/keeya/p/9218352.html
网友评论