-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_3.py
More file actions
43 lines (35 loc) · 894 Bytes
/
question_3.py
File metadata and controls
43 lines (35 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# @Time : 2018/5/5 下午3:06
# @Author : cancan
# @File : question_3.py
# @Function : 反转链表
"""
Question:
反转一个单链表。
Example:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
Follow up:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
"""
# 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 not head or not head.next:
return head
def f(node, pre):
t = node.next
node.next = pre
if not t:
return node
else:
return f(t, node)
return f(head, None)