题目描述
blablabla
样例
blablabla
算法1
() $O(n^2)$
blablabla
时间复杂度分析:blablabla
Python 代码
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None or head.next is None:
return head
cur = None
while head:
tmp = head.next # 保存当前节点下一节点 可能为0 所以最后head会move到0 返回cur
head.next = cur # 指向上一个循环的节点
cur = head # 保存目前节点
head = tmp # head指针往下移动
return cur