#how would we go about splicing elements from one LinkedList into another?

16 messages · Page 1 of 1 (latest)

carmine forge
#

hi folks! i am wondering -- is there is a good way to splice elements from one LinkedList into another? how would I mirror something like: u.splice(u.len().., v.drain(0..idx)); in a situation where instead of two Vec we have two LinkedList?

carmine forge
#

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?

wintry wren
#

It’s very cheap to split and very cheap to swap so I think you’re good