Skip to content

Commit 3438187

Browse files
authored
Added tasks-86-100
1 parent 808d5f8 commit 3438187

File tree

16 files changed

+458
-0
lines changed

16 files changed

+458
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
332332

333333
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
334334
|-|-|-|-|-|-
335+
| 0100 |[Same Tree](src/main/python/g0001_0100/s0100_same_tree/Solution0100.py)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
335336
| 0101 |[Symmetric Tree](src/main/python/g0101_0200/s0101_symmetric_tree/Solution0101.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
336337

337338
#### Day 16 Design
@@ -464,6 +465,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
464465
| 0094 |[Binary Tree Inorder Traversal](src/main/python/g0001_0100/s0094_binary_tree_inorder_traversal/Solution0094.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
465466
| 0102 |[Binary Tree Level Order Traversal](src/main/python/g0101_0200/s0102_binary_tree_level_order_traversal/Solution0102.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
466467
| 0543 |[Diameter of Binary Tree](src/main/python/g0501_0600/s0543_diameter_of_binary_tree/Solution0543.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 3 | 94.30
468+
| 0100 |[Same Tree](src/main/python/g0001_0100/s0100_same_tree/Solution0100.py)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
467469
| 0226 |[Invert Binary Tree](src/main/python/g0201_0300/s0226_invert_binary_tree/Solution0226.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
468470
| 0104 |[Maximum Depth of Binary Tree](src/main/python/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution0104.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, LeetCode_75_Binary_Tree/DFS, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00
469471
| 0124 |[Binary Tree Maximum Path Sum](src/main/python/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution0124.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 11 | 91.40
@@ -524,6 +526,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
524526

525527
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
526528
|-|-|-|-|-|-
529+
| 0088 |[Merge Sorted Array](src/main/python/g0001_0100/s0088_merge_sorted_array/Solution0088.py)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers | 0 | 100.00
527530
| 0027 |[Remove Element](src/main/python/g0001_0100/s0027_remove_element/Solution0027.py)| Easy | Array, Two_Pointers | 0 | 100.00
528531
| 0026 |[Remove Duplicates from Sorted Array](src/main/python/g0001_0100/s0026_remove_duplicates_from_sorted_array/Solution0026.py)| Easy | Top_Interview_Questions, Array, Two_Pointers | 0 | 100.00
529532
| 0080 |[Remove Duplicates from Sorted Array II](src/main/python/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/Solution0080.py)| Medium | Array, Two_Pointers | 73 | 96.40
@@ -597,17 +600,20 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
597600
| 0002 |[Add Two Numbers](src/main/python/g0001_0100/s0002_add_two_numbers/Solution0002.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)), AI_can_be_used_to_solve_the_task | 0 | 100.00
598601
| 0021 |[Merge Two Sorted Lists](src/main/python/g0001_0100/s0021_merge_two_sorted_lists/Solution0021.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
599602
| 0138 |[Copy List with Random Pointer](src/main/python/g0101_0200/s0138_copy_list_with_random_pointer/Solution0138.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 38 | 77.31
603+
| 0092 |[Reverse Linked List II](src/main/python/g0001_0100/s0092_reverse_linked_list_ii/Solution0092.py)| Medium | Linked_List | 0 | 100.00
600604
| 0025 |[Reverse Nodes in k-Group](src/main/python/g0001_0100/s0025_reverse_nodes_in_k_group/Solution0025.py)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
601605
| 0019 |[Remove Nth Node From End of List](src/main/python/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution0019.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 0 | 100.00
602606
| 0082 |[Remove Duplicates from Sorted List II](src/main/python/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/Solution0082.py)| Medium | Two_Pointers, Linked_List | 0 | 100.00
603607
| 0061 |[Rotate List](src/main/python/g0001_0100/s0061_rotate_list/Solution0061.py)| Medium | Two_Pointers, Linked_List | 0 | 100.00
608+
| 0086 |[Partition List](src/main/python/g0001_0100/s0086_partition_list/Solution0086.py)| Medium | Two_Pointers, Linked_List | 0 | 100.00
604609
| 0146 |[LRU Cache](src/main/python/g0101_0200/s0146_lru_cache/LRUCache.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 113 | 84.08
605610

606611
#### Top Interview 150 Binary Tree General
607612

608613
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
609614
|-|-|-|-|-|-
610615
| 0104 |[Maximum Depth of Binary Tree](src/main/python/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution0104.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, LeetCode_75_Binary_Tree/DFS, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00
616+
| 0100 |[Same Tree](src/main/python/g0001_0100/s0100_same_tree/Solution0100.py)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
611617
| 0226 |[Invert Binary Tree](src/main/python/g0201_0300/s0226_invert_binary_tree/Solution0226.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
612618
| 0101 |[Symmetric Tree](src/main/python/g0101_0200/s0101_symmetric_tree/Solution0101.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
613619
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/python/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution0105.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Big_O_Time_O(N)_Space_O(N) | 4 | 73.84
@@ -719,6 +725,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
719725
| 0064 |[Minimum Path Sum](src/main/python/g0001_0100/s0064_minimum_path_sum/Solution0064.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 15 | 60.38
720726
| 0063 |[Unique Paths II](src/main/python/g0001_0100/s0063_unique_paths_ii/Solution0063.py)| Medium | Array, Dynamic_Programming, Matrix | 0 | 100.00
721727
| 0005 |[Longest Palindromic Substring](src/main/python/g0001_0100/s0005_longest_palindromic_substring/Solution0005.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 47 | 97.38
728+
| 0097 |[Interleaving String](src/main/python/g0001_0100/s0097_interleaving_string/Solution0097.py)| Medium | String, Dynamic_Programming | 37 | 90.93
722729
| 0072 |[Edit Distance](src/main/python/g0001_0100/s0072_edit_distance/Solution0072.py)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, LeetCode_75_DP/Multidimensional, Big_O_Time_O(n^2)_Space_O(n2) | 23 | 98.84
723730
| 0221 |[Maximal Square](src/main/python/g0201_0300/s0221_maximal_square/Solution0221.py)| Medium | Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 129 | 64.49
724731

@@ -735,6 +742,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
735742
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
736743
|-|-|-|-|-|-
737744
| 0001 |[Two Sum](src/main/python/g0001_0100/s0001_two_sum/Solution0001.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 0 | 100.00
745+
| 0088 |[Merge Sorted Array](src/main/python/g0001_0100/s0088_merge_sorted_array/Solution0088.py)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers | 0 | 100.00
738746

739747
#### Day 3 Array
740748

@@ -1689,9 +1697,14 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
16891697
| 0104 |[Maximum Depth of Binary Tree](src/main/python/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution0104.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, LeetCode_75_Binary_Tree/DFS, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00
16901698
| 0102 |[Binary Tree Level Order Traversal](src/main/python/g0101_0200/s0102_binary_tree_level_order_traversal/Solution0102.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_BFS, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
16911699
| 0101 |[Symmetric Tree](src/main/python/g0101_0200/s0101_symmetric_tree/Solution0101.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
1700+
| 0100 |[Same Tree](src/main/python/g0001_0100/s0100_same_tree/Solution0100.py)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Level_2_Day_15_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General | 0 | 100.00
16921701
| 0098 |[Validate Binary Search Tree](src/main/python/g0001_0100/s0098_validate_binary_search_tree/Solution0098.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
1702+
| 0097 |[Interleaving String](src/main/python/g0001_0100/s0097_interleaving_string/Solution0097.py)| Medium | String, Dynamic_Programming, Top_Interview_150_Multidimensional_DP | 37 | 90.93
16931703
| 0096 |[Unique Binary Search Trees](src/main/python/g0001_0100/s0096_unique_binary_search_trees/Solution0096.py)| Medium | Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
16941704
| 0094 |[Binary Tree Inorder Traversal](src/main/python/g0001_0100/s0094_binary_tree_inorder_traversal/Solution0094.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1705+
| 0092 |[Reverse Linked List II](src/main/python/g0001_0100/s0092_reverse_linked_list_ii/Solution0092.py)| Medium | Linked_List, Top_Interview_150_Linked_List | 0 | 100.00
1706+
| 0088 |[Merge Sorted Array](src/main/python/g0001_0100/s0088_merge_sorted_array/Solution0088.py)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_I_Day_2_Array, Top_Interview_150_Array/String | 0 | 100.00
1707+
| 0086 |[Partition List](src/main/python/g0001_0100/s0086_partition_list/Solution0086.py)| Medium | Two_Pointers, Linked_List, Top_Interview_150_Linked_List | 0 | 100.00
16951708
| 0084 |[Largest Rectangle in Histogram](src/main/python/g0001_0100/s0084_largest_rectangle_in_histogram/Solution0084.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Stack, Monotonic_Stack, Big_O_Time_O(n_log_n)_Space_O(log_n) | 63 | 99.53
16961709
| 0082 |[Remove Duplicates from Sorted List II](src/main/python/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/Solution0082.py)| Medium | Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Algorithm_II_Day_3_Two_Pointers, Top_Interview_150_Linked_List | 0 | 100.00
16971710
| 0080 |[Remove Duplicates from Sorted Array II](src/main/python/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/Solution0080.py)| Medium | Array, Two_Pointers, Udemy_Arrays, Top_Interview_150_Array/String | 73 | 96.40
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# #Medium #Two_Pointers #Linked_List #Top_Interview_150_Linked_List
2+
# #2025_09_13_Time_0_ms_(100.00%)_Space_17.98_MB_(21.24%)
3+
4+
from typing import Optional
5+
6+
class ListNode:
7+
def __init__(self, val=0, next=None):
8+
self.val = val
9+
self.next = next
10+
11+
class Solution:
12+
def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
13+
n_head = ListNode(0)
14+
n_tail = ListNode(0)
15+
ptr = n_tail
16+
temp = n_head
17+
while head is not None:
18+
n_next = head.next
19+
if head.val < x:
20+
n_head.next = head
21+
n_head = n_head.next
22+
else:
23+
n_tail.next = head
24+
n_tail = n_tail.next
25+
head = n_next
26+
n_tail.next = None
27+
n_head.next = ptr.next
28+
return temp.next
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
from Solution0086 import Solution, ListNode
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_partition(self):
6+
head = ListNode(1)
7+
head.next = ListNode(4)
8+
head.next.next = ListNode(3)
9+
head.next.next.next = ListNode(2)
10+
head.next.next.next.next = ListNode(5)
11+
head.next.next.next.next.next = ListNode(2)
12+
result = Solution().partition(head, 3)
13+
# Convert to list for comparison
14+
result_list = []
15+
current = result
16+
while current:
17+
result_list.append(current.val)
18+
current = current.next
19+
self.assertEqual(result_list, [1, 2, 2, 4, 3, 5])
20+
21+
def test_partition2(self):
22+
head = ListNode(2)
23+
head.next = ListNode(1)
24+
result = Solution().partition(head, 2)
25+
# Convert to list for comparison
26+
result_list = []
27+
current = result
28+
while current:
29+
result_list.append(current.val)
30+
current = current.next
31+
self.assertEqual(result_list, [1, 2])
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
86\. Partition List
2+
3+
Medium
4+
5+
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`.
6+
7+
You should **preserve** the original relative order of the nodes in each of the two partitions.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/01/04/partition.jpg)
12+
13+
**Input:** head = [1,4,3,2,5,2], x = 3
14+
15+
**Output:** [1,2,2,4,3,5]
16+
17+
**Example 2:**
18+
19+
**Input:** head = [2,1], x = 2
20+
21+
**Output:** [1,2]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range `[0, 200]`.
26+
* `-100 <= Node.val <= 100`
27+
* `-200 <= x <= 200`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# #Easy #Top_Interview_Questions #Array #Sorting #Two_Pointers #Data_Structure_I_Day_2_Array
2+
# #Top_Interview_150_Array/String #2025_09_13_Time_0_ms_(100.00%)_Space_17.78_MB_(71.73%)
3+
4+
from typing import List
5+
6+
class Solution:
7+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
8+
i = m - 1
9+
j = len(nums1) - 1
10+
p2 = n - 1
11+
while p2 >= 0:
12+
if i >= 0 and nums1[i] > nums2[p2]:
13+
nums1[j] = nums1[i]
14+
j -= 1
15+
i -= 1
16+
else:
17+
nums1[j] = nums2[p2]
18+
j -= 1
19+
p2 -= 1
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
from Solution0088 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_merge(self):
6+
array = [1, 2, 3, 0, 0, 0]
7+
Solution().merge(array, 3, [2, 5, 6], 3)
8+
self.assertEqual(array, [1, 2, 2, 3, 5, 6])
9+
10+
def test_merge2(self):
11+
array = [1]
12+
Solution().merge(array, 1, [], 0)
13+
self.assertEqual(array, [1])
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
88\. Merge Sorted Array
2+
3+
Easy
4+
5+
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively.
6+
7+
**Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**.
8+
9+
The final sorted array should not be returned by the function, but instead be _stored inside the array_ `nums1`. To accommodate this, `nums1` has a length of `m + n`, where the first `m` elements denote the elements that should be merged, and the last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
14+
15+
**Output:** [1,2,2,3,5,6]
16+
17+
**Explanation:** The arrays we are merging are [1,2,3] and [2,5,6]. The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
18+
19+
**Example 2:**
20+
21+
**Input:** nums1 = [1], m = 1, nums2 = [], n = 0
22+
23+
**Output:** [1]
24+
25+
**Explanation:** The arrays we are merging are [1] and []. The result of the merge is [1].
26+
27+
**Example 3:**
28+
29+
**Input:** nums1 = [0], m = 0, nums2 = [1], n = 1
30+
31+
**Output:** [1]
32+
33+
**Explanation:** The arrays we are merging are [] and [1]. The result of the merge is [1]. Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
34+
35+
**Constraints:**
36+
37+
* `nums1.length == m + n`
38+
* `nums2.length == n`
39+
* `0 <= m, n <= 200`
40+
* `1 <= m + n <= 200`
41+
* <code>-10<sup>9</sup> <= nums1[i], nums2[j] <= 10<sup>9</sup></code>
42+
43+
**Follow up:** Can you come up with an algorithm that runs in `O(m + n)` time?
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# #Medium #Linked_List #Top_Interview_150_Linked_List
2+
# #2025_09_13_Time_0_ms_(100.00%)_Space_18.11_MB_(14.44%)
3+
4+
from typing import Optional
5+
6+
class ListNode:
7+
def __init__(self, val=0, next=None):
8+
self.val = val
9+
self.next = next
10+
11+
class Solution:
12+
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
13+
if left == right:
14+
return head
15+
prev = None
16+
temp = head
17+
start = None
18+
k = left
19+
while temp is not None and k > 1:
20+
prev = temp
21+
temp = temp.next
22+
k -= 1
23+
if left > 1 and prev is not None:
24+
prev.next = None
25+
prev1 = None
26+
start = temp
27+
while temp is not None and right - left >= 0:
28+
prev1 = temp
29+
temp = temp.next
30+
right -= 1
31+
if prev1 is not None:
32+
prev1.next = None
33+
if left > 1 and prev is not None:
34+
prev.next = self._reverse(start)
35+
else:
36+
head = self._reverse(start)
37+
prev = head
38+
while prev.next is not None:
39+
prev = prev.next
40+
prev.next = temp
41+
return head
42+
43+
def _reverse(self, head: Optional[ListNode]) -> Optional[ListNode]:
44+
p = head
45+
r = None
46+
while p is not None:
47+
q = p.next
48+
p.next = r
49+
r = p
50+
p = q
51+
return r

0 commit comments

Comments
 (0)