-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap_nodes_in_pairs.rs
59 lines (57 loc) · 1.47 KB
/
swap_nodes_in_pairs.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
///
/// Problem: Swap Nodes in Pairs
///
/// Given a linked list, swap every two adjacent nodes and return its head.
/// You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed).
///
/// Example 1:
/// Input: head = [1,2,3,4]
/// Output: [2,1,4,3]
///
/// Example 2:
/// Input: head = []
/// Output: []
///
/// Example 3:
/// Input: head = [1]
/// Output: [1]
///
/// # Solution:
/// This solution uses iterative pointer manipulation to swap nodes in pairs. By keeping track of the previous node
/// and swapping the current pair, the process is efficient and avoids recursion.
///
/// Time complexity: O(n)
/// Space complexity: O(1)
// 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_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
match head {
Some(mut h) => {
match h.next {
Some(mut n) => {
h.next = Solution::swap_pairs(n.next);
n.next = Some(h);
Some(n)
},
_ => Some(h),
}
},
_ => head,
}
}
}