File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode:
3
+ # def __init__(self, val=0, next=None):
4
+ # self.val = val
5
+ # self.next = next
6
+ # TC, SC: O(n), O(1)
7
+ class Solution :
8
+ def reverseList (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
9
+ if not head :
10
+ return None
11
+
12
+ if not head .next :
13
+ return head
14
+
15
+ prev_node = head
16
+ node = prev_node .next
17
+ prev_node .next = None
18
+
19
+ while node .next :
20
+ next_node = node .next
21
+ node .next = prev_node
22
+ prev_node = node
23
+ node = next_node
24
+
25
+ node .next = prev_node
26
+
27
+ return node
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isValid (self , s : str ) -> bool :
3
+ arr = []
4
+
5
+ for c in s :
6
+ if c in ["(" , "[" , "{" ]:
7
+ arr .append (c )
8
+ continue
9
+ else :
10
+ if not arr :
11
+ return False
12
+ b = arr .pop ()
13
+ if b == "(" and c == ")" :
14
+ continue
15
+ if b == "[" and c == "]" :
16
+ continue
17
+ if b == "{" and c == "}" :
18
+ continue
19
+ return False
20
+ if arr :
21
+ return False
22
+ else :
23
+ return True
You can’t perform that action at this time.
0 commit comments