[LeetCode]206 反转链表

题目描述

反转一个单链表。

示例1:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

示例2:

输入: s = "foo", t = "bar"
输出: false

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
tmp=None
while head:
cur=head.next
head.next=tmp
tmp=head
head=cur
return tmp