-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path206.py
40 lines (35 loc) · 1.2 KB
/
206.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
__________________________________________________________________________________________________
sample 20 ms submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
curr = head
new_head = None
while curr:
next_node = curr.next
curr.next = new_head
new_head = curr
curr = next_node
return new_head
__________________________________________________________________________________________________
sample 14236 kb submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head is None: return None
pre, cur, nxt = None, head, head.next
while cur:
cur.next = pre
pre = cur
cur = nxt
if cur: nxt = cur.next
return pre
__________________________________________________________________________________________________