简体   繁体   中英

Function to move elements to the end of a doubly linked list up to a certain index

For example, given a double linked list with {4, 5, 6, 7}, and an index 2, the function should result in the node being {6, 7, 4, 5}. Is it possible to do this without creating a temp node?

My implementation has a head and tail, both set to null. Nodes can be accessed with next/previous.

Any help would be greatly appreciated!

I'm not sure about how you implemented it. But this is how I would do it as a pseudocode.

tail.prev.next = head.next
head.next.prev = tail.prev
tail.prev = head.next
head.next = head.next.next
tail.prev.next = tail
head.next.prev = head

Assumptions:

1) Head and Tail nodes are present and they are linked to the first node and the last node, respectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM