Delete Node
by Isai Damier

#=======================================================================
# Author: Isai Damier
# Title: Singly Linked List
# Project: geekviewpoint
# Package: datastructure
#
# Description: A LinkedList is a data structure that allows access
#   to a collection of data using pointers/references. While an
#   array can also be defined as above, LinkedLists and arrays differ
#   in how they are stored in memory and in the operations they
#   allow. Unlike an array that must be stored in a block of memory,
#   the nodes of a LinkedList can be stored anywhere because each
#   node has a reference to the node that succeeds it. Because the
#   nodes are stored so loosely, inserting nodes into a LinkedList
#   is easy; whereas in an array, all the succeeding elements must
#   be shifted. Of course, insertion also means changing the size of
#   the array, which means creating the entire array anew.
#
#   Perhaps the greatest beauty of LinkedList is that it allows
#   accessing an entire sequence of nodes using only one variable:
#   a reference to the first node in the sequence.
#
#   Countless operations can be performed on LinkedLists. Following
#   are a few, ranging from the common to the very interesting.
#=======================================================================
  #=====================================================================
  # Description: Retrieve and remove the specified node.
  #
  #   One of the drawbacks of LinkedList is that
  #   it allows no random access. Therefore deleting a random
  #   node is a O(n) process because the list must first be
  #   traversed to find the node containing the element.
  #
  # Technical Details:
  #   0] If node is the self.head, simply delete from self.head and
  #      return: O(1).
  #   1] If node is the self.tail, simply delete from self.tail and
  #      return: O(1).
  #   2] Otherwise, find the node's parent.
  #   3] If the nod is found, give parent custody of node's child.
  #   4] Return the node.
  #=====================================================================
 
 import collections
class SinglyLinkedList( object ):

  def __init__( self ):
    self.head , self.tail = None, None

  def deleteNode( self, n ) :
    if None == n:
      return False

    if n == self.head:
      self.deleteFromHead()
      return True

    if n == self.tail:
      self.deleteFromTail()
      return True

    parent , curr = None, self.head
    while None != curr and n != curr:
      parent = curr
      curr = curr.next

    if n == curr:
      parent.next = curr.next
      return True

    return False

class Node( object ):

  def __init__( self, data, next = None ):
    self.data = data
    self.next = next

import unittest
from algorithms.SinglyLinkedList import SinglyLinkedList
import random

class Test( unittest.TestCase ):
  #=====================================================================
  # Test of delete method, of class SinglyLinkedList.
  #=====================================================================
  def testDelete_Node( self ):
    tape = [9, 4, 5, 2, 1, 12, 6, 7, 4, 8, 3, 0, 16, 19, 11]
    linkedList = SinglyLinkedList()
    for i in range( len( tape ) ):
      linkedList.addToTail( tape[i] )

    n = linkedList.find( tape[0] )
    self.assertEquals( True, linkedList.deleteNode( n ) )
    n = linkedList.find( tape[5] )
    self.assertEquals( True, linkedList.deleteNode( n ) )
    last = len( tape ) - 1
    n = linkedList.find( tape[last] )
    self.assertEquals( True, linkedList.deleteNode( n ) )
    self.assertEquals( False, linkedList.deleteNode( None ) )
    expected = [4, 5, 2, 1, 6, 7, 4, 8, 3, 0, 16, 19]
    self.assertEquals( expected, linkedList.toArray() )