Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head == None:
return None
# L M R
# M -> L
L = None
M = None
R = head
while R.next != None:
L = M
M = R
R = R.next
M.next = L
R.next = M
return R
来源:https://blog.csdn.net/huyun9666/article/details/100941222