# A Question about DoubleLinkedList.remove(\_:)

**URL:** <https://forums.kodeco.com/t/a-question-about-doublelinkedlist-remove/177764>\
**Category:** Data Structures & Algorithms in Swift\
**Created:** [September 3, 2022, 4:52am UTC](https://forums.kodeco.com/t/a-question-about-doublelinkedlist-remove/177764 "2022-09-03T04:52:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ys\_xs](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ys_xs/32/105896_2.png) [@ys\_xs](https://forums.kodeco.com/u/ys_xs)\
**Post date:** [September 3, 2022, 4:52am UTC](https://forums.kodeco.com/t/a-question-about-doublelinkedlist-remove/177764/1 "2022-09-03T04:52:06Z")

</div>

In the method of removing nodes, After completing this operation: `prev.next = next` && `next?.previous = prev `

it seems that the node has no more strong reference. At this time, the node should have been destroyed, but in the end why can ”return the node value“?

here is code in the resource file:  
QueueChallenge → Sources → DoubleLinkedList.swift → remove function

```auto
public func remove(_ node: Node<T>) -> T {
    let prev = node.previous
    let next = node.next
    
    if let prev = prev {
      prev.next = next // 👈 here
    } else {
      head = next
    }
    
    next?.previous = prev // 👈 here
    
    if next == nil {
      tail = prev
    }
    
    node.previous = nil 
    node.next = nil
    
    return node.value // 👈 here
  }

```

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [September 6, 2022, 7:31pm UTC](https://forums.kodeco.com/t/a-question-about-doublelinkedlist-remove/177764/2 "2022-09-06T19:31:30Z")

</div>

What chapter/section does this pertain to?

---

<div class="post-metadata">

**Author:** ![dipid](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dipid/32/102075_2.png) [@dipid](https://forums.kodeco.com/u/dipid)\
**Post date:** [October 12, 2022, 7:17pm UTC](https://forums.kodeco.com/t/a-question-about-doublelinkedlist-remove/177764/3 "2022-10-12T19:17:26Z")

</div>

Delete a node from a singly connected linked list. You will not be given the head of the list.  
You will be given a node to be deleted. That node will not be the tail node in the linked list.  
The node to be deleted will always be in the linked list.  
Delete the node in place and no return value needed.  
Input: head = [1,5,2,3,4], node = 2  
Output: [1,5,3,4]  
class DeleteANodeInLinkedList {  
public void deleteNode(ListNode node) {  
while(node!=null){  
if(node.next!=null){  
node.val=node.next.val;  
if(node.next.next==null){  
node.next=null;  
}  
}  
node=node.next;  
}  
}  
}  
Found this linked list problem in kuickcode dot com , there are many algo nicely explained.
