title: Deleting a specific node from a linked list - python
body: <p>As an exercise I am trying to create my own linked list. I have created a node class and a linked list class. I have been able to create a constructor and a node adder method. I am having a lot of problems with my delete method though. Here is my code first:</p>
  
  <pre><code>class Node:
  
  def __init__(self,value,next=None):
      self.value = value
      self.next = next
  
  def getNext(self):
      return self.next
  
  def getValue(self):
      return self.value
  
  class LinkedList:
  
  def __init__(self,node):
      self.node = node
  
  def __str__(self):
      node = self.node
      s = "[Node " + str(node.getValue())
      if node.getNext() is None:
          return s + "]"
      while node.next is not None:
          s += ", Node " + str(node.next.value)
          node = node.next
      return s + "]"
  
  def add(self,newNode):
      node = self.node
      while node.next is not None:
          node = node.next
      node.next = newNode
  
  def delete(self,value):
      head = self.node
      if head.getValue() == value:
          return LinkedList(head.next)
      temp = head
      while temp.next is not None:
          if temp.next.getValue() == value:
              temp = temp.next.next
              temp.next = None
              return LinkedList(head)
          temp = temp.next
      return "Sorry node is not here!"
  </code></pre>
  
  <p>My delete function you give it a specific value that a node in the list may or may not have and then remove the node and return the list unless the node does not exist then it will return a message. I have the case where you are removing the head by just returning a constructor, although to be honest I am not a big fan of creating a new instance of the linked list. Removing a node from anywhere else does not work though as I test it by printing out the list and it shows no change to the linked list. Any help would be greatly appreciated!</p>