File tree 3 files changed +115
-1
lines changed
3 files changed +115
-1
lines changed Original file line number Diff line number Diff line change @@ -84,7 +84,7 @@ class Solution {
84
84
* @param {ListNode} head
85
85
* @return {ListNode}
86
86
*/
87
- var reverseList = function (head ) {
87
+ var reverseList = function (head ) {
88
88
let node = head;
89
89
let pre = null ;
90
90
while (node) {
@@ -146,6 +146,68 @@ public:
146
146
};
147
147
```
148
148
149
+ ### ** TypeScript**
150
+
151
+ ``` ts
152
+ /**
153
+ * Definition for singly-linked list.
154
+ * class ListNode {
155
+ * val: number
156
+ * next: ListNode | null
157
+ * constructor(val?: number, next?: ListNode | null) {
158
+ * this.val = (val===undefined ? 0 : val)
159
+ * this.next = (next===undefined ? null : next)
160
+ * }
161
+ * }
162
+ */
163
+
164
+ function reverseList(head : ListNode | null ): ListNode | null {
165
+ let cur = head ;
166
+ let pre = null ;
167
+ while (cur != null ) {
168
+ const temp = cur .next ;
169
+ cur .next = pre ;
170
+ pre = cur ;
171
+ cur = temp ;
172
+ }
173
+ return pre ;
174
+ }
175
+ ```
176
+
177
+ ### ** Rust**
178
+
179
+ ``` rust
180
+ // Definition for singly-linked list.
181
+ // #[derive(PartialEq, Eq, Clone, Debug)]
182
+ // pub struct ListNode {
183
+ // pub val: i32,
184
+ // pub next: Option<Box<ListNode>>
185
+ // }
186
+ //
187
+ // impl ListNode {
188
+ // #[inline]
189
+ // fn new(val: i32) -> Self {
190
+ // ListNode {
191
+ // next: None,
192
+ // val
193
+ // }
194
+ // }
195
+ // }
196
+ impl Solution {
197
+ pub fn reverse_list (head : Option <Box <ListNode >>) -> Option <Box <ListNode >> {
198
+ let mut pre = None ;
199
+ let mut cur = head ;
200
+
201
+ while let Some (mut node ) = cur {
202
+ cur = node . next. take ();
203
+ node . next = pre . take ();
204
+ pre = Some (node );
205
+ }
206
+ pre
207
+ }
208
+ }
209
+ ```
210
+
149
211
### ** ...**
150
212
151
213
```
Original file line number Diff line number Diff line change
1
+ // Definition for singly-linked list.
2
+ // #[derive(PartialEq, Eq, Clone, Debug)]
3
+ // pub struct ListNode {
4
+ // pub val: i32,
5
+ // pub next: Option<Box<ListNode>>
6
+ // }
7
+ //
8
+ // impl ListNode {
9
+ // #[inline]
10
+ // fn new(val: i32) -> Self {
11
+ // ListNode {
12
+ // next: None,
13
+ // val
14
+ // }
15
+ // }
16
+ // }
17
+ impl Solution {
18
+ pub fn reverse_list ( head : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
19
+ let mut pre = None ;
20
+ let mut cur = head;
21
+
22
+ while let Some ( mut node) = cur {
23
+ cur = node. next . take ( ) ;
24
+ node. next = pre. take ( ) ;
25
+ pre = Some ( node) ;
26
+ }
27
+ pre
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * class ListNode {
4
+ * val: number
5
+ * next: ListNode | null
6
+ * constructor(val?: number, next?: ListNode | null) {
7
+ * this.val = (val===undefined ? 0 : val)
8
+ * this.next = (next===undefined ? null : next)
9
+ * }
10
+ * }
11
+ */
12
+
13
+ function reverseList ( head : ListNode | null ) : ListNode | null {
14
+ let cur = head ;
15
+ let pre = null ;
16
+ while ( cur != null ) {
17
+ const temp = cur . next ;
18
+ cur . next = pre ;
19
+ pre = cur ;
20
+ cur = temp ;
21
+ }
22
+ return pre ;
23
+ }
You can’t perform that action at this time.
0 commit comments