Skip to content

Commit 4e5f15c

Browse files
committed
Merge branch 'master' of github.com:cch123/leetcode-rust
2 parents e4c6e2b + dd4189b commit 4e5f15c

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

S0024-swap-nodes-in-pairs/Cargo.lock

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

S0024-swap-nodes-in-pairs/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "S0024-swap-nodes-in-pairs"
3+
version = "0.1.0"
4+
authors = ["Xargin <cao1988228@163.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
leetcode_prelude="0.1"

S0024-swap-nodes-in-pairs/src/main.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Definition for singly-linked list.
2+
use leetcode_prelude::*;
3+
4+
struct Solution;
5+
use std::mem::{replace, swap};
6+
7+
impl Solution {
8+
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
9+
let mut head = head;
10+
let mut cursor = &mut head;
11+
while cursor.is_some() && cursor.as_ref().unwrap().next.is_some() {
12+
let mut n2 = replace(&mut cursor.as_mut().unwrap().next, None);
13+
swap(
14+
&mut cursor.as_mut().unwrap().next,
15+
&mut n2.as_mut().unwrap().next,
16+
);
17+
swap(&mut n2.as_mut().unwrap().next, cursor);
18+
swap(cursor, &mut n2);
19+
cursor = &mut cursor.as_mut().unwrap().next.as_mut().unwrap().next;
20+
}
21+
22+
head
23+
}
24+
}
25+
/*
26+
struct Solution;
27+
28+
impl Solution {
29+
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
30+
let mut head = head;
31+
let mut cursor = &mut head;
32+
while cursor.is_some() && cursor.as_ref().unwrap().next.is_some() {
33+
let mut next = None;
34+
// cursor.next = None; tmp = cursor.next
35+
std::mem::swap(&mut cursor.as_mut().unwrap().next, &mut next);
36+
// cursor.next = tmp.next; tmp.next = None;
37+
std::mem::swap(
38+
&mut cursor.as_mut().unwrap().next,
39+
&mut next.as_mut().unwrap().next,
40+
);
41+
// tmp.next = cursor
42+
std::mem::swap(cursor, &mut next.as_mut().unwrap().next);
43+
//cursor = &mut next;
44+
std::mem::swap(cursor, &mut next);
45+
cursor = &mut cursor.as_mut().unwrap().next.as_mut().unwrap().next;
46+
}
47+
48+
//println!("head : {:?}", &head);
49+
head
50+
}
51+
}
52+
*/
53+
54+
fn main() {
55+
let l = linkedlist![1, 2, 3, 4];
56+
println!("{:?}", Solution::swap_pairs(l));
57+
}
58+
59+
/*
60+
// Definition for singly-linked list.
61+
// #[derive(PartialEq, Eq, Debug)]
62+
// pub struct ListNode {
63+
// pub val: i32,
64+
// pub next: Option<Box<ListNode>>
65+
// }
66+
//
67+
// impl ListNode {
68+
// #[inline]
69+
// fn new(val: i32) -> Self {
70+
// ListNode {
71+
// next: None,
72+
// val
73+
// }
74+
// }
75+
// }
76+
impl Solution {
77+
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
78+
use std::mem::swap;
79+
80+
if head.is_none() {
81+
return None;
82+
}
83+
84+
let mut head = Solution::swap_head(head.unwrap());
85+
let mut current = &mut head.next;
86+
87+
let mut should_swap = true;
88+
while let Some(ref mut node) = current {
89+
if should_swap {
90+
Solution::swap_nodes(node);
91+
}
92+
current = &mut node.next;
93+
should_swap = !should_swap;
94+
}
95+
96+
Some(head)
97+
}
98+
99+
// swaps the two nodes after parent.
100+
fn swap_nodes(parent: &mut Box<ListNode>) {
101+
use std::mem::swap;
102+
103+
// parent -> a -> b -> c => parent -> b -> a -> c
104+
105+
let mut a = None;
106+
let mut b = None;
107+
let mut c = None; // rest of the list
108+
109+
swap(&mut parent.next, &mut a); // now we have: parent, a-> b -> c
110+
111+
if let Some(ref mut a) = a {
112+
swap(&mut a.next, &mut b); // parent, a, b -> c
113+
}
114+
115+
if let Some(ref mut b) = b {
116+
swap(&mut b.next, &mut c); // parent, a, b, c
117+
}
118+
else {
119+
// we reached the end of the list, put things back the way they were and return
120+
parent.next = a;
121+
return;
122+
}
123+
124+
// now reassemble the list in the new order, parent -> b -> a -> c
125+
126+
if let Some(ref mut a) = a {
127+
a.next = c; // parent, b, a -> c
128+
}
129+
130+
if let Some(ref mut b) = b {
131+
b.next = a; // parent, b -> a -> c
132+
}
133+
134+
parent.next = b; // parent -> b -> a -> c
135+
}
136+
137+
// swap the head of the list and the next element.
138+
// returns the new head
139+
fn swap_head(mut head: Box<ListNode>) -> Box<ListNode> {
140+
use std::mem::swap;
141+
142+
if head.next.is_none() {
143+
head
144+
}
145+
else {
146+
let mut next = None;
147+
swap(&mut head.next, &mut next);
148+
let mut next = next.unwrap();
149+
let mut rest = next.next;
150+
head.next = rest;
151+
next.next = Some(head);
152+
next
153+
}
154+
}
155+
}
156+
*/

0 commit comments

Comments
 (0)