Skip to content

Commit 91082d4

Browse files
committed
feat: reverse-linked-list, valid-parentheses 풀이
1 parent 4e6204f commit 91082d4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

reverse-linked-list/saysimple.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

valid-parentheses/saysimple.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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

0 commit comments

Comments
 (0)