Skip to content

Commit

Permalink
[#16] add: LinkedList::Append
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Oct 3, 2023
1 parent da18976 commit 05bd22a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions linked_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ func (list *LinkedList[T]) Front() Option[*T] {
return Some[*T](&list.head.value)
}

// Moves all elements from other to the end of the list.
// This reuses all the nodes from other and moves them into self. After this operation, other becomes empty.
// This operation should compute in O(1) time and O(1) memory.
func (list *LinkedList[T]) Append(other *LinkedList[T]) {
if other.head == nil {
return
}

if list.head == nil {
list.head = other.head
list.tail = other.tail
list.len = other.len
} else {
list.tail.next = other.head
other.head.prev = list.tail
list.tail = other.tail
list.len += other.len
}

other.Clear()
}

// into_iter
func (list *LinkedList[T]) IntoIter() Iterator[T] {
return &LinkedListIter[T]{
Expand Down

0 comments on commit 05bd22a

Please sign in to comment.