File tree Expand file tree Collapse file tree 1 file changed +11
-35
lines changed Expand file tree Collapse file tree 1 file changed +11
-35
lines changed Original file line number Diff line number Diff line change 1
1
# Definition for singly-linked list.
2
- # class ListNode(object) :
2
+ # class ListNode:
3
3
# def __init__(self, val=0, next=None):
4
4
# self.val = val
5
5
# 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 ]:
35
8
prev = None
36
- while (curr != None ):
37
- next = curr .next
9
+ curr = head
10
+ nextt = None
11
+ while (curr ):
12
+ nextt = curr .next
38
13
curr .next = prev
39
14
prev = curr
40
- curr = next
41
- return prev
42
-
15
+ curr = nextt
16
+ head = prev
17
+ return head
18
+
43
19
You can’t perform that action at this time.
0 commit comments