給定一個(gè)單鏈表揩抡,將其反轉(zhuǎn)的方法一般有兩種户侥,第一種為常規(guī)遍歷法,通過設(shè)置兩個(gè)空節(jié)點(diǎn)來傳值峦嗤。
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;
}
}
第二種為遞歸法蕊唐,屬于系統(tǒng)壓棧
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