206 反转链表 python
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
仙衣眠云碧岚袍,一襟潇洒,两袖飘飘。玉墨舒心春酝瓢,行也逍遥,坐也逍遥。