翻转单链表
题目描述: 给定一个单链表1-2-3-4-5,翻转成5-4-3-2-1。
public ListNode
reverse(ListNode head
){
if(head
==null
) return head
;
ListNode tail
=head
;
head
=head
.next
;
tail
.next
=null
;
while(head
!=null
){
ListNode temp
=head
.next
;
head
.next
=tail
;
tail
=head
;
head
=temp
;
}
return tail
;
}
转载请注明原文地址: https://lol.8miu.com/read-2911.html