title: How to recyle the linked list node immediately after delete it in python
body: <p>I want to delete a node in linked list and recyle it immediately in Python, but I don't know how to achieve that, here is example code:</p>
  
  <pre><code># Definition for singly-linked list.
  class ListNode:
      def __init__(self, x):
          self.val = x
          self.next = None
  
  # delete node
  def delete_node_after(pre_node):
      deleted_node = pre_node.next
      pre_node.next = pre_node.next.next
      del deleted_node        # incorrect, it is only delete `deleted_node` reference to l2
  
  def test():
      l1 = ListNode(1)
      l2 = ListNode(2)
      l3 = ListNode(3)
  
      l1.next = l2
      l2.next = l3
  
      delete_node_after(l2)
  
      print(l1.next.val)
      print(l2.val)           # l2 is still here
  </code></pre>
  
  <p>The reason I want to do this is that this Linked List will have a long lifecyle, and I want release deleted node's memory immediately. </p>
  
  <p>Or I only can wait for Python to auto recycle it by Reference Count after some while?</p>