206. Reverse Linked List
题目:
https://leetcode.com/problems/reverse-linked-list/
难度:
Easy
用三个指针,分别指向prev,cur 和 nxt,然后loop一圈还算比较简单.
待尝试递归.
# 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
"""
prev = None
cur = head
while(cur):
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
return prev