#how would we go about splicing elements from one LinkedList into another?
16 messages · Page 1 of 1 (latest)
You can split off one linked list
https://doc.rust-lang.org/std/collections/struct.LinkedList.html#method.split_off
And then append its contents to the other https://doc.rust-lang.org/std/collections/struct.LinkedList.html#method.append
A doubly-linked list with owned nodes.
thanks! it looks like it will work, but i am not sure i understand the example :```rust
use std::collections::LinkedList;
let mut d = LinkedList::new();
d.push_front(1);
d.push_front(2);
d.push_front(3);
let mut split = d.split_off(2);
assert_eq!(split.pop_front(), Some(1));
assert_eq!(split.pop_front(), None);
it looks like the list is, after pushing, [3, 2, 1]
oh
i get it now
when we split_off(2), we are returning a LinkedList with a single element 1
and the original list becomes [3,2]
ok
thanks!
split_off(2) is zero indexed
ok
i will split_off and then append
thanks!
is there any way to split_off the front instead of the back? will it be expensive to split_off the back and then swap in case we just want to split off the front?
It’s very cheap to split and very cheap to swap so I think you’re good