8000 Added tasks 138-160 · LeetCode-in-Php/LeetCode-in-Php@14a847f · GitHub
[go: up one dir, main page]

Skip to content

Commit 14a847f

Browse files
authored
Added tasks 138-160
1 parent 99357ec commit 14a847f

File tree

32 files changed

+1305
-0
lines changed

32 files changed

+1305
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
9191

9292
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9393
|-|-|-|-|-|-
94+
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 9 | 73.47
9495

9596
### Binary Search II
9697

@@ -229,6 +230,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
229230

230231
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
231232
|-|-|-|-|-|-
233+
| 0152 |[Maximum Product Subarray](src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 15 | 92.86
232234

233235
#### Day 7
234236

@@ -245,6 +247,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
245247

246248
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
247249
|-|-|-|-|-|-
250+
| 0139 |[Word Break](src/main/php/g0101_0200/s0139_word_break/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 7 | 75.00
248251
| 0042 |[Trapping Rain Water](src/main/php/g0001_0100/s0042_trapping_rain_water/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 29 | 69.15
249252

250253
#### Day 10
@@ -448,6 +451,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
448451

449452
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
450453
|-|-|-|-|-|-
454+
| 0138 |[Copy List with Random Pointer](src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 2300 | 60.00
451455

452456
#### Day 15
453457

@@ -469,6 +473,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
469473

470474
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
471475
|-|-|-|-|-|-
476+
| 0155 |[Min Stack](src/main/php/g0101_0200/s0155_min_stack/MinStack.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 19 | 100.00
472477

473478
#### Day 19
474479

@@ -626,6 +631,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
626631

627632
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
628633
|-|-|-|-|-|-
634+
| 0142 |[Linked List Cycle II](src/main/php/g0101_0200/s0142_linked_list_cycle_ii/Solution.php)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 7 | 100.00
629635

630636
#### Day 5 Greedy
631637

@@ -710,6 +716,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
710716

711717
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
712718
|-|-|-|-|-|-
719+
| 0148 |[Sort List](src/main/php/g0101_0200/s0148_sort_list/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(log(N))_Space_O(log(N)) | 63 | 97.30
713720

714721
#### Day 5 Greedy
715722

@@ -757,6 +764,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
757764

758765
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
759766
|-|-|-|-|-|-
767+
| 0152 |[Maximum Product Subarray](src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 15 | 92.86
760768

761769
#### Day 14 Sliding Window/Two Pointer
762770

@@ -775,6 +783,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
775783

776784
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
777785
|-|-|-|-|-|-
786+
| 0155 |[Min Stack](src/main/php/g0101_0200/s0155_min_stack/MinStack.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 19 | 100.00
778787

779788
#### Day 17 Interval
780789

@@ -823,6 +832,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
823832
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
824833
|-|-|-|-|-|-
825834
| 0033 |[Search in Rotated Sorted Array](src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 7 | 83.17
835+
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 9 | 73.47
826836

827837
#### Udemy Arrays
828838

@@ -867,8 +877,13 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
867877
|-|-|-|-|-|-
868878
| 0114 |[Flatten Binary Tree to Linked List](src/main/php/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.php)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 3 | 100.00
869879
| 0024 |[Swap Nodes in Pairs](src/main/php/g0001_0100/s0024_swap_nodes_in_pairs/Solution.php)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 3 | 85.71
880+
| 0142 |[Linked List Cycle II](src/main/php/g0101_0200/s0142_linked_list_cycle_ii/Solution.php)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 7 | 100.00
881+
| 0141 |[Linked List Cycle](src/main/php/g0101_0200/s0141_linked_list_cycle/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 8 | 90.60
870882
| 0021 |[Merge Two Sorted Lists](src/main/php/g0001_0100/s0021_merge_two_sorted_lists/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 3 | 90.57
883+
| 0160 |[Intersection of Two Linked Lists](src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 30 | 81.67
884+
| 0138 |[Copy List with Random Pointer](src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 2300 | 60.00
871885
| 0025 |[Reverse Nodes in k-Group](src/main/php/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.php)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 3 | 100.00
886+
| 0146 |[LRU Cache](src/main/php/g0101_0200/s0146_lru_cache/LRUCache.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 402 | 41.18
872887

873888
#### Udemy Tree Stack Queue
874889

@@ -894,6 +909,8 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
894909

895910
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
896911
|-|-|-|-|-|-
912+
| 0139 |[Word Break](src/main/php/g0101_0200/s0139_word_break/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 7 | 75.00
913+
| 0152 |[Maximum Product Subarray](src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 15 | 92.86
897914
| 0070 |[Climbing Stairs](src/main/php/g0001_0100/s0070_climbing_stairs/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 3 | 82.81
898915
| 0064 |[Minimum Path Sum](src/main/php/g0001_0100/s0064_minimum_path_sum/Solution.php)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 20 | 81.48
899916
| 0072 |[Edit Distance](src/main/php/g0001_0100/s0072_edit_distance/Solution.php)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 29 | 75.00
@@ -918,6 +935,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
918935

919936
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
920937
|-|-|-|-|-|-
938+
| 0155 |[Min Stack](src/main/php/g0101_0200/s0155_min_stack/MinStack.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 19 | 100.00
921939

922940
### Data Structure I
923941

@@ -959,6 +977,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
959977

960978
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
961979
|-|-|-|-|-|-
980+
| 0141 |[Linked List Cycle](src/main/php/g0101_0200/s0141_linked_list_cycle/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 8 | 90.60
962981
| 0021 |[Merge Two Sorted Lists](src/main/php/g0001_0100/s0021_merge_two_sorted_lists/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 3 | 90.57
963982

964983
#### Day 8 Linked List
@@ -1061,11 +1080,13 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
10611080
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10621081
|-|-|-|-|-|-
10631082
| 0002 |[Add Two Numbers](src/main/php/g0001_0100/s0002_add_two_numbers/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 13 | 73.83
1083+
| 0142 |[Linked List Cycle II](src/main/php/g0101_0200/s0142_linked_list_cycle_ii/Solution.php)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 7 | 100.00
10641084

10651085
#### Day 11 Linked List
10661086

10671087
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10681088
|-|-|-|-|-|-
1089+
| 0160 |[Intersection of Two Linked Lists](src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 30 | 81.67
10691090

10701091
#### Day 12 Linked List
10711092

@@ -1083,6 +1104,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
10831104

10841105
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10851106
|-|-|-|-|-|-
1107+
| 0155 |[Min Stack](src/main/php/g0101_0200/s0155_min_stack/MinStack.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 19 | 100.00
10861108

10871109
#### Day 15 Tree
10881110

@@ -1213,6 +1235,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
12131235

12141236
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12151237
|-|-|-|-|-|-
1238+
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 9 | 73.47
12161239

12171240
#### Day 3 Two Pointers
12181241

@@ -1289,6 +1312,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
12891312

12901313
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12911314
|-|-|-|-|-|-
1315+
| 0139 |[Word Break](src/main/php/g0101_0200/s0139_word_break/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 7 | 75.00
12921316

12931317
#### Day 16 Dynamic Programming
12941318

@@ -1325,6 +1349,16 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
13251349

13261350
| # | Title | Difficulty | Tag | Time, ms | Time, %
13271351
|------|----------------|-------------|-------------|----------|---------
1352+
| 0160 |[Intersection of Two Linked Lists](src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Udemy_Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 30 | 81.67
1353+
| 0155 |[Min Stack](src/main/php/g0101_0200/s0155_min_stack/MinStack.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Data_Structure_II_Day_14_Stack_Queue, Programming_Skills_II_Day_18, Level_2_Day_16_Design, Udemy_Design, Big_O_Time_O(1)_Space_O(N) | 19 | 100.00
1354+
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_I_Day_12, Udemy_Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 9 | 73.47
1355+
| 0152 |[Maximum Product Subarray](src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Dynamic_Programming_I_Day_6, Level_2_Day_13_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 15 | 92.86
1356+
| 0148 |[Sort List](src/main/php/g0101_0200/s0148_sort_list/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Level_2_Day_4_Linked_List, Big_O_Time_O(log(N))_Space_O(log(N)) | 63 | 97.30
1357+
| 0146 |[LRU Cache](src/main/php/g0101_0200/s0146_lru_cache/LRUCache.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Udemy_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 402 | 41.18
1358+
| 0142 |[Linked List Cycle II](src/main/php/g0101_0200/s0142_linked_list_cycle_ii/Solution.php)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_10_Linked_List, Level_1_Day_4_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 7 | 100.00
1359+
| 0141 |[Linked List Cycle](src/main/php/g0101_0200/s0141_linked_list_cycle/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_I_Day_7_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 8 | 90.60
1360+
| 0139 |[Word Break](src/main/php/g0101_0200/s0139_word_break/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_9, Udemy_Dynamic_Programming, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 7 | 75.00
1361+
| 0138 |[Copy List with Random Pointer](src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Programming_Skills_II_Day_14, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 2300 | 60.00
13281362
| 0136 |[Single Number](src/main/php/g0101_0200/s0136_single_number/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Big_O_Time_O(N)_Space_O(1) | 33 | 80.08
13291363
| 0131 |[Palindrome Partitioning](src/main/php/g0101_0200/s0131_palindrome_partitioning/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 159 | 61.90
13301364
| 0128 |[Longest Consecutive Sequence](src/main/php/g0101_0200/s0128_longest_consecutive_sequence/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 154 | 75.61
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace leetcode\com_github_leetcode\random;
4+
5+
class Node {
6+
public $val;
7+
public $next;
8+
public $random;
9+
10+
function __construct($val = 0, $next = null, $random = null) {
11+
$this->val = $val;
12+
$this->next = $next;
13+
$this->random = $random;
14+
}
15+
16+
function __toString() {
17+
$result = array();
18+
$result2 = array();
19+
array_push($result2, strval($this->val));
20+
if ($this->random == null) {
21+
array_push($result2, "null");
22+
} else {
23+
array_push($result2, strval($this->random->val));
24+
}
25+
array_push($result, "[" . implode(",", $result2) . "]");
26+
$curr = $this->next;
27+
while ($curr != null) {
28+
$result3 = array();
29+
array_push($result3, strval($curr->val));
30+
if ($curr->random == null) {
31+
array_push($result3, "null");
32+
} else {
33+
$randomIndex = 0;
34+
$curr2 = $this;
35+
while ($curr2->next != null && $curr2 != $curr->random) {
36+
$randomIndex += 1;
37+
$curr2 = $curr2->next;
38+
}
39+
array_push($result3, strval($randomIndex));
40+
}
41+
array_push($result, "[" . implode(",", $result3) . "]");
42+
$curr = $curr->next;
43+
}
44+
return "[" . implode(",", $result) . "]";
45+
}
46+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace leetcode\g0101_0200\s0138_copy_list_with_random_pointer;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Linked_List
6+
// #Programming_Skills_II_Day_14 #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(N)
7+
// #2023_12_17_Time_2300_ms_(60.00%)_Space_22.7_MB_(60.00%)
8+
9+
use leetcode\com_github_leetcode\random\Node;
10+
11+
/**
12+
* Definition for a Node.
13+
* class Node {
14+
* public $val = null;
15+
* public $next = null;
16+
* public $random = null;
17+
* function __construct($val = 0) {
18+
* $this->val = $val;
19+
* $this->next = null;
20+
* $this->random = null;
21+
* }
22+
* }
23+
*/
24+
class Solution {
25+
/**
26+
* @param Node $head
27+
* @return Node
28+
*/
29+
public function copyRandomList($head) {
30+
if ($head == null) {
31+
return null;
32+
}
33+
$curr = $head;
34+
while ($curr != null) {
35+
$clonedNode = new Node($curr->val);
36+
$clonedNode->next = $curr->next;
37+
$curr->next = $clonedNode;
38+
$curr = $clonedNode->next;
39+
}
40+
$curr = $head;
41+
while ($curr != null) {
42+
if ($curr->random != null) {
43+
$curr->next->random = $curr->random->next;
44+
} else {
45+
$curr->next->random = null;
46+
}
47+
$curr = $curr->next->next;
48+
}
49+
$curr = $head;
50+
$newHead = null;
51+
while ($curr != null) {
52+
if ($newHead == null) {
53+
$clonedNode = $curr->next;
54+
$newHead = $clonedNode;
55+
} else {
56+
$clonedNode = $curr->next;
57+
}
58+
$curr->next = $clonedNode->next;
59+
if ($curr->next != null) {
60+
$clonedNode->next = $curr->next->next;
61+
} else {
62+
$clonedNode->next = null;
63+
}
64+
$curr = $curr->next;
65+
}
66+
return $newHead;
67+
}
68+
}

0 commit comments

Comments
 (0)
0