解题思路:
比较简单,具体可以看代码,借助一些中间变量,做一个遍历就可以达到效果。
具体代码如下:
# 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 not head or not head.next:
return head
pre = None
cur = head
follow = head.next
while cur:
cur.next = pre
pre = cur
cur = follow
if follow:
follow = follow.next
return pre