链接:206. 反转链表 - 力扣(LeetCode)

直接写就可以,python主要多一个链表结构的自定义

 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, val=0, next=None):
 4 #         self.val = val
 5 #         self.next = next
 6 class Solution(object):
 7     def reverseList(self, head):
 8         """
 9         :type head: Optional[ListNode]
10         :rtype: Optional[ListNode]
11         """
12         if head == None or head.next == None:
13             return head
14         nextNode = head.next
15         head.next = None
16         while nextNode:
17             nextNode2 = nextNode.next
18             nextNode.next = head
19             head = nextNode
20             nextNode = nextNode2
21         return head