Skip to content

Commit e9316e3

Browse files
committed
Time: 46 ms (54.85%), Space: 15.4 MB (98.14%) - LeetHub
1 parent 6d63ca3 commit e9316e3

File tree

1 file changed

+11
-35
lines changed

1 file changed

+11
-35
lines changed
Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,19 @@
11
# Definition for singly-linked list.
2-
# class ListNode(object):
2+
# class ListNode:
33
# def __init__(self, val=0, next=None):
44
# self.val = val
55
# self.next = next
6-
7-
8-
# Time=O(n)
9-
# Space=O(n)
10-
11-
class Solution(object):
12-
def reverseList(self, head):
13-
"""
14-
:type head: ListNode
15-
:rtype: ListNode
16-
"""
17-
#------------------Approach 1-----------------------#
18-
19-
20-
# arr=[]
21-
# node = head
22-
# temp=head
23-
# while(node):
24-
# arr.append(node.val)
25-
# node=node.next
26-
# for i in range(len(arr)-1,-1,-1):
27-
# temp.val=arr[i]
28-
# temp=temp.next
29-
# return head
30-
31-
32-
#------------------Approach 2-----------------------#
33-
34-
curr=head
6+
class Solution:
7+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
358
prev=None
36-
while(curr!=None):
37-
next=curr.next
9+
curr=head
10+
nextt=None
11+
while(curr):
12+
nextt=curr.next
3813
curr.next=prev
3914
prev=curr
40-
curr=next
41-
return prev
42-
15+
curr=nextt
16+
head=prev
17+
return head
18+
4319

0 commit comments

Comments
 (0)