反转链表
反转一个单链表。
示例:
输入:1->2->3->4->5->NULL
输出:5->4->3->2->1->NULL
解题思路:
参考博客
代码:
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head==None or head.next==None:
return head
pre=None
next=None
while(head!=None):
next=head.next
head.next=pre
pre=head
head=next
return pre