Skip to content

Commit f5180db

Browse files
authored
feat: add rust solution to lc problem: No.0148 (#1802)
No.0148.Sort List
1 parent 0b8278d commit f5180db

File tree

3 files changed

+180
-2
lines changed

3 files changed

+180
-2
lines changed

solution/0100-0199/0148.Sort List/README.md

+61
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,67 @@ function sortList(head: ListNode | null): ListNode | null {
366366
}
367367
```
368368

369+
### **Rust**
370+
371+
```rust
372+
// Definition for singly-linked list.
373+
// #[derive(PartialEq, Eq, Clone, Debug)]
374+
// pub struct ListNode {
375+
// pub val: i32,
376+
// pub next: Option<Box<ListNode>>
377+
// }
378+
//
379+
// impl ListNode {
380+
// #[inline]
381+
// fn new(val: i32) -> Self {
382+
// ListNode {
383+
// next: None,
384+
// val
385+
// }
386+
// }
387+
// }
388+
impl Solution {
389+
pub fn sort_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
390+
fn merge(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
391+
match (l1, l2) {
392+
(None, Some(node)) | (Some(node), None) => Some(node),
393+
(Some(mut node1), Some(mut node2)) => {
394+
if node1.val < node2.val {
395+
node1.next = merge(node1.next.take(), Some(node2));
396+
Some(node1)
397+
} else {
398+
node2.next = merge(Some(node1), node2.next.take());
399+
Some(node2)
400+
}
401+
}
402+
_ => None,
403+
}
404+
}
405+
406+
fn sort(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
407+
if head.is_none() || head.as_ref().unwrap().next.is_none() {
408+
return head;
409+
}
410+
let mut head = head;
411+
let mut length = 0;
412+
let mut cur = &head;
413+
while cur.is_some() {
414+
length += 1;
415+
cur = &cur.as_ref().unwrap().next;
416+
}
417+
let mut cur = &mut head;
418+
for _ in 0..(length / 2 - 1) {
419+
cur = &mut cur.as_mut().unwrap().next;
420+
}
421+
let right = cur.as_mut().unwrap().next.take();
422+
423+
merge(sort(head), sort(right))
424+
}
425+
sort(head)
426+
}
427+
}
428+
```
429+
369430
### **...**
370431

371432
```

solution/0100-0199/0148.Sort List/README_EN.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,14 @@ public class Solution {
321321

322322
function sortList(head: ListNode | null): ListNode | null {
323323
if (head == null || head.next == null) return head;
324-
// 快慢指针定位中点
324+
// slow and fast pointer to locate the middle node
325325
let slow: ListNode = head,
326326
fast: ListNode = head.next;
327327
while (fast != null && fast.next != null) {
328328
slow = slow.next;
329329
fast = fast.next.next;
330330
}
331-
// 归并排序
331+
// merge sort
332332
let mid: ListNode = slow.next;
333333
slow.next = null;
334334
let l1: ListNode = sortList(head);
@@ -350,6 +350,67 @@ function sortList(head: ListNode | null): ListNode | null {
350350
}
351351
```
352352

353+
### **Rust**
354+
355+
```rust
356+
// Definition for singly-linked list.
357+
// #[derive(PartialEq, Eq, Clone, Debug)]
358+
// pub struct ListNode {
359+
// pub val: i32,
360+
// pub next: Option<Box<ListNode>>
361+
// }
362+
//
363+
// impl ListNode {
364+
// #[inline]
365+
// fn new(val: i32) -> Self {
366+
// ListNode {
367+
// next: None,
368+
// val
369+
// }
370+
// }
371+
// }
372+
impl Solution {
373+
pub fn sort_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
374+
fn merge(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
375+
match (l1, l2) {
376+
(None, Some(node)) | (Some(node), None) => Some(node),
377+
(Some(mut node1), Some(mut node2)) => {
378+
if node1.val < node2.val {
379+
node1.next = merge(node1.next.take(), Some(node2));
380+
Some(node1)
381+
} else {
382+
node2.next = merge(Some(node1), node2.next.take());
383+
Some(node2)
384+
}
385+
}
386+
_ => None,
387+
}
388+
}
389+
390+
fn sort(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
391+
if head.is_none() || head.as_ref().unwrap().next.is_none() {
392+
return head;
393+
}
394+
let mut head = head;
395+
let mut length = 0;
396+
let mut cur = &head;
397+
while cur.is_some() {
398+
length += 1;
399+
cur = &cur.as_ref().unwrap().next;
400+
}
401+
let mut cur = &mut head;
402+
for _ in 0..(length / 2 - 1) {
403+
cur = &mut cur.as_mut().unwrap().next;
404+
}
405+
let right = cur.as_mut().unwrap().next.take();
406+
407+
merge(sort(head), sort(right))
408+
}
409+
sort(head)
410+
}
411+
}
412+
```
413+
353414
### **...**
354415

355416
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 sort_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
19+
fn merge(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
20+
match (l1, l2) {
21+
(None, Some(node)) | (Some(node), None) => Some(node),
22+
(Some(mut node1), Some(mut node2)) => {
23+
if node1.val < node2.val {
24+
node1.next = merge(node1.next.take(), Some(node2));
25+
Some(node1)
26+
} else {
27+
node2.next = merge(Some(node1), node2.next.take());
28+
Some(node2)
29+
}
30+
}
31+
_ => None,
32+
}
33+
}
34+
35+
fn sort(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
36+
if head.is_none() || head.as_ref().unwrap().next.is_none() {
37+
return head;
38+
}
39+
let mut head = head;
40+
let mut length = 0;
41+
let mut cur = &head;
42+
while cur.is_some() {
43+
length += 1;
44+
cur = &cur.as_ref().unwrap().next;
45+
}
46+
let mut cur = &mut head;
47+
for _ in 0..(length / 2 - 1) {
48+
cur = &mut cur.as_mut().unwrap().next;
49+
}
50+
let right = cur.as_mut().unwrap().next.take();
51+
52+
merge(sort(head), sort(right))
53+
}
54+
sort(head)
55+
}
56+
}

0 commit comments

Comments
 (0)