-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswapping-nodes-in-a-linked-list.rs
68 lines (56 loc) · 1.81 KB
/
swapping-nodes-in-a-linked-list.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#![allow(dead_code, unused, unused_variables)]
fn main() {}
struct Solution;
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
impl Solution {
/// 普通方法,先获取所有的值,然后再组装新的链表返回
pub fn swap_nodes1(mut head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
let mut v = vec![];
while let Some(i) = head.take() {
v.push(i.val);
head = i.next;
}
let n = v.len() - k as usize;
v.swap(k as usize - 1, n);
let mut head = None;
for i in (0..v.len()).rev() {
let mut node = ListNode::new(v[i]);
if head.is_none() {
head = Some(Box::new(node));
} else {
node.next = head.take();
head = Some(Box::new(node));
}
}
head
}
/// 快慢指针,先快指针到第k个值的时候,慢指针从第一个元素开始移动
/// 快指针到达最后一个元素的时候,慢指针刚好在倒数第k个元素的位置
pub fn swap_nodes(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
let mut index = 1;
let (mut fast, mut slow, mut k_node) = (head.as_ref(), head.as_ref(), None);
while fast.is_some() {
if index == k {
k_node = fast;
} else if index > k {
slow = slow.unwrap().next.as_ref();
}
fast = fast.unwrap().next.as_ref();
index += 1;
}
let _f = k_node.unwrap().val;
let _s = slow.unwrap().val;
head
}
}