File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ Descripttion: 反转链表
3+ version: 1
4+ Author: Jason
5+ Date: 2020-12-08 14:24:37
6+ LastEditors: Jason
7+ LastEditTime: 2020-12-08 17:54:27
8+ '''
9+
10+ # -*- coding:utf-8 -*-
11+ # class ListNode:
12+ # def __init__(self, x):
13+ # self.val = x
14+ # self.next = None
15+
16+
17+ class Solution :
18+ # 返回ListNode
19+ def ReverseList (self , pHead ):
20+ # write code here
21+ if not pHead :
22+ return pHead
23+ pre_node = None
24+ next_node = None
25+ while pHead :
26+ next_node = pHead .next
27+ pHead .next = pre_node
28+ pre_node = pHead
29+ pHead = next_node
30+ return pre_node
31+
32+ def ReverseList2 (self , pHead ):
33+ # 用了额外的内存空间
34+ if not pHead :
35+ return pHead
36+ cur = pHead
37+ nodes = list ()
38+ while cur :
39+ nodes .append (cur )
40+ cur = cur .next
41+ for i in range (len (nodes ) - 1 , 0 , - 1 ):
42+ nodes [i ].next = nodes [i - 1 ]
43+ nodes [0 ].next = None
44+ return nodes [- 1 ]
You can’t perform that action at this time.
0 commit comments