Skip to content

Commit b0da995

Browse files
committed
linkedlist solves
1 parent 56f0332 commit b0da995

File tree

7 files changed

+149
-20
lines changed

7 files changed

+149
-20
lines changed

Easy/3349.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def hasIncreasingSubarrays(self, nums: List[int], k: int) -> bool:
6+
prev_len = 0
7+
start = 0
8+
for i in range(1, len(nums) + 1):
9+
if i == len(nums) or nums[i] <= nums[i - 1]:
10+
length = i - start
11+
if prev_len >= k and length >= k or length >= 2 * k:
12+
return True
13+
prev_len = length
14+
start = i
15+
return False

Medium/138.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for a Node.
2+
from typing import Optional
3+
4+
5+
class Node:
6+
def __init__(self, x: int, next: "Node" = None, random: "Node" = None):
7+
self.val = int(x)
8+
self.next = next
9+
self.random = random
10+
11+
12+
class Solution:
13+
def copyRandomList(self, head: "Optional[Node]") -> "Optional[Node]":
14+
or_to_new = {None: None}
15+
curr = head
16+
while curr:
17+
copy = Node(curr.val)
18+
or_to_new[curr] = copy
19+
curr = curr.next
20+
21+
curr = head
22+
while curr:
23+
copy = or_to_new[curr]
24+
copy.next = or_to_new[curr.next]
25+
copy.random = or_to_new[curr.random]
26+
curr = curr.next
27+
return or_to_new[head]

Medium/19.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@
22
from typing import Optional
33

44

5-
class Node:
6-
def __init__(self, x: int, next: "Node" = None, random: "Node" = None):
7-
self.val = int(x)
5+
class ListNode:
6+
def __init__(self, val=0, next=None):
7+
self.val = val
88
self.next = next
9-
self.random = random
109

1110

1211
class Solution:
13-
def copyRandomList(self, head: "Optional[Node]") -> "Optional[Node]":
14-
mapp = {None: None}
15-
curr = head
16-
while curr:
17-
copy = Node(curr.val)
18-
mapp[curr] = copy
19-
curr = curr.next
12+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
13+
res = ListNode(0, head)
14+
dummy = res
2015

21-
curr = head
22-
while curr:
23-
copy = mapp[curr]
24-
copy.next = mapp[curr.next]
25-
copy.random = mapp[curr.random]
26-
curr = curr.next
16+
for _ in range(n):
17+
head = head.next
2718

28-
return mapp[head]
19+
while head:
20+
head = head.next
21+
dummy = dummy.next
22+
23+
dummy.next = dummy.next.next
24+
return res.next

Medium/61.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Definition for singly-linked list.
2+
from typing import Optional
3+
4+
5+
class ListNode:
6+
def __init__(self, val=0, next=None):
7+
self.val = val
8+
self.next = next
9+
10+
11+
class Solution:
12+
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
13+
if not head or not head.next or k == 0:
14+
return head
15+
16+
# find length and tail
17+
length = 1
18+
tail = head
19+
while tail.next:
20+
tail = tail.next
21+
length += 1
22+
23+
k = k % length
24+
if k == 0:
25+
return head
26+
27+
curr = head
28+
for _ in range(length - k - 1):
29+
curr = curr.next
30+
31+
new_head = curr.next
32+
curr.next = None
33+
tail.next = head
34+
return new_head

Medium/82.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for singly-linked list.
2+
from typing import Optional
3+
4+
5+
class ListNode:
6+
def __init__(self, val=0, next=None):
7+
self.val = val
8+
self.next = next
9+
10+
11+
class Solution:
12+
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
13+
res = ListNode(-1, head)
14+
dummy = res
15+
prev = res
16+
cur = head
17+
while cur and cur.next:
18+
if cur.val == cur.next.val:
19+
while cur.next and cur.val == cur.next.val:
20+
cur = cur.next
21+
prev.next = cur.next
22+
else:
23+
prev = prev.next
24+
cur = cur.next
25+
26+
return res.next

Medium/92.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import Optional
2+
3+
4+
class ListNode:
5+
def __init__(self, val=0, next=None):
6+
self.val = val
7+
self.next = next
8+
9+
10+
class Solution:
11+
def reverseBetween(
12+
self, head: Optional[ListNode], left: int, right: int
13+
) -> Optional[ListNode]:
14+
15+
if not head or left == right:
16+
return head
17+
18+
dummy = ListNode(0, head)
19+
prev = dummy
20+
21+
for _ in range(left - 1):
22+
prev = prev.next
23+
24+
cur = prev.next
25+
for _ in range(right - left):
26+
temp = cur.next
27+
cur.next = temp.next
28+
temp.next = prev.next
29+
prev.next = temp
30+
31+
return dummy.next

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ This repository contains my solutions to various LeetCode problems. Each solutio
1717

1818
### Easy Problems
1919

20-
- **Total Solved:** [46]
20+
- **Total Solved:** [47]
2121

2222
### Medium Problems
2323

24-
- **Total Solved:** [118]
24+
- **Total Solved:** [121]
2525

2626
### Hard Problems
2727

0 commit comments

Comments
 (0)