206. Reverse Linked List
# 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 head is None or head.next is None:
return head
pre = None
cur = head
temp = head.next
while(temp):
cur.next = pre
pre = cur
cur = temp
temp = temp.next
cur.next = pre
return cur
Last updated