-
Notifications
You must be signed in to change notification settings - Fork 0
/
python206.py
46 lines (34 loc) · 1.08 KB
/
python206.py
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
44
45
46
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
current = head
prev = None
while current:
tmp_next = current.next
current.next = prev
prev = current
current = tmp_next
return prev
'''
new_list = ListNode(0)
new_list.next = None
current = head
nodes_list = []
if not head:
return None
while current:
nodes_list.append(current)
current = current.next
for i in range(len(nodes_list)-1, -1, -1):
if not new_list.next:
new_list = nodes_list[i]
new_ptr = new_list
new_ptr.next = nodes_list[i]
new_ptr = new_ptr.next
new_ptr.next = None
return new_list
'''