206. 反转链表 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
- """
- pre = None
- current = head
- while current:
- temp =current.next
- current.next = pre
- pre = current
- current = temp
- return pre
来源: http://www.bubuko.com/infodetail-3090325.html