1
1
/*
2
- single linked list merge
3
- This problem requires you to merge two ordered singly linked lists into one ordered singly linked list
2
+ single linked list merge
3
+ This problem requires you to merge two ordered singly linked lists into one ordered singly linked list
4
4
*/
5
- // I AM NOT DONE
6
5
7
6
use std:: fmt:: { self , Display , Formatter } ;
8
7
use std:: ptr:: NonNull ;
@@ -16,12 +15,30 @@ struct Node<T> {
16
15
17
16
impl < T > Node < T > {
18
17
fn new ( t : T ) -> Node < T > {
19
- Node {
20
- val : t,
21
- next : None ,
22
- }
18
+ Node { val : t, next : None }
19
+ }
20
+ }
21
+
22
+ trait UnSafeRead {
23
+ type ValType ;
24
+ type NextType ;
25
+ fn get_val ( & self ) -> Self :: ValType ;
26
+ fn get_next ( & self ) -> Option < Self :: NextType > ;
27
+ }
28
+
29
+ impl < T : Copy > UnSafeRead for NonNull < Node < T > > {
30
+ type ValType = T ;
31
+ type NextType = NonNull < Node < T > > ;
32
+
33
+ fn get_val ( & self ) -> Self :: ValType {
34
+ unsafe { ( * self . as_ptr ( ) ) . val }
35
+ }
36
+
37
+ fn get_next ( & self ) -> Option < Self :: NextType > {
38
+ unsafe { ( * self . as_ptr ( ) ) . next }
23
39
}
24
40
}
41
+
25
42
#[ derive( Debug ) ]
26
43
struct LinkedList < T > {
27
44
length : u32 ,
@@ -56,28 +73,66 @@ impl<T> LinkedList<T> {
56
73
self . length += 1 ;
57
74
}
58
75
59
- pub fn get ( & mut self , index : i32 ) -> Option < & T > {
60
- self . get_ith_node ( self . start , index)
76
+ pub fn get_val ( & mut self , index : i32 ) -> Option < & T > {
77
+ self . get_ith_val ( self . start , index)
61
78
}
62
79
63
- fn get_ith_node ( & mut self , node : Option < NonNull < Node < T > > > , index : i32 ) -> Option < & T > {
80
+ fn get_ith_val ( & mut self , node : Option < NonNull < Node < T > > > , index : i32 ) -> Option < & T > {
64
81
match node {
65
82
None => None ,
66
83
Some ( next_ptr) => match index {
67
84
0 => Some ( unsafe { & ( * next_ptr. as_ptr ( ) ) . val } ) ,
85
+ _ => self . get_ith_val ( unsafe { ( * next_ptr. as_ptr ( ) ) . next } , index - 1 ) ,
86
+ } ,
87
+ }
88
+ }
89
+ fn get_ith_node (
90
+ & mut self ,
91
+ node : Option < NonNull < Node < T > > > ,
92
+ index : i32 ,
93
+ ) -> Option < NonNull < Node < T > > > {
94
+ match node {
95
+ None => None ,
96
+ Some ( next_ptr) => match index {
97
+ 0 => Some ( next_ptr) ,
68
98
_ => self . get_ith_node ( unsafe { ( * next_ptr. as_ptr ( ) ) . next } , index - 1 ) ,
69
99
} ,
70
100
}
71
101
}
72
- pub fn merge ( list_a : LinkedList < T > , list_b : LinkedList < T > ) -> Self
73
- {
74
- //TODO
75
- Self {
76
- length : 0 ,
77
- start : None ,
78
- end : None ,
102
+
103
+ pub fn merge ( mut list_a : LinkedList < T > , mut list_b : LinkedList < T > ) -> Self
104
+ where
105
+ T : PartialOrd + Copy ,
106
+ {
107
+ let mut list_c = Self :: new ( ) ;
108
+ let node_a = list_a. start ;
109
+ let node_b = list_b. start ;
110
+
111
+ let mut node_a = list_a. get_ith_node ( node_a, 0 ) ;
112
+ let mut node_b = list_b. get_ith_node ( node_b, 0 ) ;
113
+
114
+ while let ( Some ( a_node) , Some ( b_node) ) = ( node_a. as_ref ( ) , node_b. as_ref ( ) ) {
115
+ if a_node. get_val ( ) > b_node. get_val ( ) {
116
+ list_c. add ( b_node. get_val ( ) ) ;
117
+ node_b = list_b. get_ith_node ( node_b, 1 ) ;
118
+ } else {
119
+ list_c. add ( a_node. get_val ( ) ) ;
120
+ node_a = list_a. get_ith_node ( node_a, 1 ) ;
121
+ }
122
+ }
123
+
124
+ while let Some ( a_node) = node_a. as_ref ( ) {
125
+ list_c. add ( a_node. get_val ( ) ) ;
126
+ node_a = list_a. get_ith_node ( node_a, 1 ) ;
127
+ }
128
+
129
+ while let Some ( b_node) = node_b. as_ref ( ) {
130
+ list_c. add ( b_node. get_val ( ) ) ;
131
+ node_b = list_b. get_ith_node ( node_b, 1 ) ;
79
132
}
80
- }
133
+
134
+ list_c
135
+ }
81
136
}
82
137
83
138
impl < T > Display for LinkedList < T >
@@ -130,44 +185,44 @@ mod tests {
130
185
131
186
#[ test]
132
187
fn test_merge_linked_list_1 ( ) {
133
- let mut list_a = LinkedList :: < i32 > :: new ( ) ;
134
- let mut list_b = LinkedList :: < i32 > :: new ( ) ;
135
- let vec_a = vec ! [ 1 , 3 , 5 , 7 ] ;
136
- let vec_b = vec ! [ 2 , 4 , 6 , 8 ] ;
137
- let target_vec = vec ! [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ;
138
-
139
- for i in 0 ..vec_a. len ( ) {
140
- list_a. add ( vec_a[ i] ) ;
141
- }
142
- for i in 0 ..vec_b. len ( ) {
143
- list_b. add ( vec_b[ i] ) ;
144
- }
145
- println ! ( "list a {} list b {}" , list_a, list_b) ;
146
- let mut list_c = LinkedList :: < i32 > :: merge ( list_a, list_b) ;
147
- println ! ( "merged List is {}" , list_c) ;
148
- for i in 0 ..target_vec. len ( ) {
149
- assert_eq ! ( target_vec[ i] , * list_c. get ( i as i32 ) . unwrap( ) ) ;
150
- }
151
- }
152
- #[ test]
153
- fn test_merge_linked_list_2 ( ) {
154
- let mut list_a = LinkedList :: < i32 > :: new ( ) ;
155
- let mut list_b = LinkedList :: < i32 > :: new ( ) ;
156
- let vec_a = vec ! [ 11 , 33 , 44 , 88 , 89 , 90 , 100 ] ;
157
- let vec_b = vec ! [ 1 , 22 , 30 , 45 ] ;
158
- let target_vec = vec ! [ 1 , 11 , 22 , 30 , 33 , 44 , 45 , 88 , 89 , 90 , 100 ] ;
159
-
160
- for i in 0 ..vec_a. len ( ) {
161
- list_a. add ( vec_a[ i] ) ;
162
- }
163
- for i in 0 ..vec_b. len ( ) {
164
- list_b. add ( vec_b[ i] ) ;
165
- }
166
- println ! ( "list a {} list b {}" , list_a, list_b) ;
167
- let mut list_c = LinkedList :: < i32 > :: merge ( list_a, list_b) ;
168
- println ! ( "merged List is {}" , list_c) ;
169
- for i in 0 ..target_vec. len ( ) {
170
- assert_eq ! ( target_vec[ i] , * list_c. get ( i as i32 ) . unwrap( ) ) ;
171
- }
172
- }
173
- }
188
+ let mut list_a = LinkedList :: < i32 > :: new ( ) ;
189
+ let mut list_b = LinkedList :: < i32 > :: new ( ) ;
190
+ let vec_a = vec ! [ 1 , 3 , 5 , 7 ] ;
191
+ let vec_b = vec ! [ 2 , 4 , 6 , 8 ] ;
192
+ let target_vec = vec ! [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ;
193
+
194
+ for i in 0 ..vec_a. len ( ) {
195
+ list_a. add ( vec_a[ i] ) ;
196
+ }
197
+ for i in 0 ..vec_b. len ( ) {
198
+ list_b. add ( vec_b[ i] ) ;
199
+ }
200
+ println ! ( "list a {} list b {}" , list_a, list_b) ;
201
+ let mut list_c = LinkedList :: < i32 > :: merge ( list_a, list_b) ;
202
+ println ! ( "merged List is {}" , list_c) ;
203
+ for i in 0 ..target_vec. len ( ) {
204
+ assert_eq ! ( target_vec[ i] , * list_c. get_val ( i as i32 ) . unwrap( ) ) ;
205
+ }
206
+ }
207
+ #[ test]
208
+ fn test_merge_linked_list_2 ( ) {
209
+ let mut list_a = LinkedList :: < i32 > :: new ( ) ;
210
+ let mut list_b = LinkedList :: < i32 > :: new ( ) ;
211
+ let vec_a = vec ! [ 11 , 33 , 44 , 88 , 89 , 90 , 100 ] ;
212
+ let vec_b = vec ! [ 1 , 22 , 30 , 45 ] ;
213
+ let target_vec = vec ! [ 1 , 11 , 22 , 30 , 33 , 44 , 45 , 88 , 89 , 90 , 100 ] ;
214
+
215
+ for i in 0 ..vec_a. len ( ) {
216
+ list_a. add ( vec_a[ i] ) ;
217
+ }
218
+ for i in 0 ..vec_b. len ( ) {
219
+ list_b. add ( vec_b[ i] ) ;
220
+ }
221
+ println ! ( "list a {} list b {}" , list_a, list_b) ;
222
+ let mut list_c = LinkedList :: < i32 > :: merge ( list_a, list_b) ;
223
+ println ! ( "merged List is {}" , list_c) ;
224
+ for i in 0 ..target_vec. len ( ) {
225
+ assert_eq ! ( target_vec[ i] , * list_c. get_val ( i as i32 ) . unwrap( ) ) ;
226
+ }
227
+ }
228
+ }
0 commit comments