|
| 1 | +/** |
| 2 | + * [24] 两两交换链表中的节点 |
| 3 | + * |
| 4 | + * 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 |
| 5 | + * |
| 6 | + * 示例 1: |
| 7 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg" style="width: 422px; height: 222px;" /> |
| 8 | + * 输入:head = [1,2,3,4] |
| 9 | + * 输出:[2,1,4,3] |
| 10 | + * |
| 11 | + * 示例 2: |
| 12 | + * |
| 13 | + * 输入:head = [] |
| 14 | + * 输出:[] |
| 15 | + * |
| 16 | + * 示例 3: |
| 17 | + * |
| 18 | + * 输入:head = [1] |
| 19 | + * 输出:[1] |
| 20 | + * |
| 21 | + * |
| 22 | + * 提示: |
| 23 | + * |
| 24 | + * 链表中节点的数目在范围 [0, 100] 内 |
| 25 | + * 0 <= Node.val <= 100 |
| 26 | + * |
| 27 | + */ |
| 28 | +pub struct Solution {} |
| 29 | +use crate::util::linked_list::{to_list, ListNode}; |
| 30 | + |
| 31 | +// problem: https://leetcode.cn/problems/swap-nodes-in-pairs/ |
| 32 | +// discuss: https://leetcode.cn/problems/swap-nodes-in-pairs/discuss/?currentPage=1&orderBy=most_votes&query= |
| 33 | + |
| 34 | +// submission codes start here |
| 35 | + |
| 36 | +// Definition for singly-linked list. |
| 37 | +// #[derive(PartialEq, Eq, Clone, Debug)] |
| 38 | +// pub struct ListNode { |
| 39 | +// pub val: i32, |
| 40 | +// pub next: Option<Box<ListNode>> |
| 41 | +// } |
| 42 | +// |
| 43 | +// impl ListNode { |
| 44 | +// #[inline] |
| 45 | +// fn new(val: i32) -> Self { |
| 46 | +// ListNode { |
| 47 | +// next: None, |
| 48 | +// val |
| 49 | +// } |
| 50 | +// } |
| 51 | +// } |
| 52 | +impl Solution { |
| 53 | + pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { |
| 54 | + let mut dummy_head = Some(Box::new(ListNode { next: head, val: 0 })); |
| 55 | + let mut p = &mut dummy_head; |
| 56 | + loop { |
| 57 | + if p.as_ref().is_none() |
| 58 | + || p.as_ref().unwrap().next.as_ref().is_none() |
| 59 | + || p.as_ref() |
| 60 | + .unwrap() |
| 61 | + .next |
| 62 | + .as_ref() |
| 63 | + .unwrap() |
| 64 | + .next |
| 65 | + .as_ref() |
| 66 | + .is_none() |
| 67 | + { |
| 68 | + break; |
| 69 | + } |
| 70 | + let mut m = p.as_mut().unwrap().next.take(); |
| 71 | + let mut n = m.as_mut().unwrap().next.take(); |
| 72 | + let mut x = n.as_mut().unwrap().next.take(); |
| 73 | + m.as_mut().unwrap().next = x; |
| 74 | + n.as_mut().unwrap().next = m; |
| 75 | + p.as_mut().unwrap().next = n; |
| 76 | + p = &mut p.as_mut().unwrap().next.as_mut().unwrap().next; |
| 77 | + } |
| 78 | + dummy_head.unwrap().next |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// submission codes end |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use super::*; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_24() { |
| 90 | + assert_eq!( |
| 91 | + Solution::swap_pairs(to_list(vec![1, 2, 3, 4])), |
| 92 | + to_list(vec![2, 1, 4, 3]) |
| 93 | + ); |
| 94 | + assert_eq!( |
| 95 | + Solution::swap_pairs(to_list(vec![1, 2, 3, 4, 5])), |
| 96 | + to_list(vec![2, 1, 4, 3, 5]) |
| 97 | + ); |
| 98 | + assert_eq!(Solution::swap_pairs(to_list(vec![])), None); |
| 99 | + assert_eq!(Solution::swap_pairs(to_list(vec![1])), to_list(vec![1])); |
| 100 | + } |
| 101 | +} |
0 commit comments