public ListNode1
reverseLinked(ListNode1 head
) {
ListNode1 cur
= head
;
ListNode1 prev
= null
;
while (cur
!= null
) {
ListNode1 nextTemp
= cur
.next
;
cur
.next
= prev
;
prev
= cur
;
cur
= nextTemp
;
}
return prev
;
}
不是太明白递归这种反转方式
举例:将
1-2-3-4-5-null
反转为
5-4-3-2-1-null
public ListNode1
reverseLinked(ListNode1 head
) {
if(head
== null
|| head
.next
== null
) return head
;
ListNode1 newHead
= reverseLinked(head
.next
);
head
.next
.next
= head
;
head
.next
= null
;
return newHead
;
}
转载请注明原文地址:https://tech.qufami.com/read-441.html