Friday, March 21, 2014

Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node.

__author__ = 'nitin'

class Node:
  def __init__(self,val,nxt):
    self.val = val
    self.nxt = nxt

def prnt(n):
  nxt = n.nxt
  print n.val
  if(nxt is not None):
    prnt(nxt)

def delete_node(n):
    prnt(n8)
    print "\n"
    if n is None or n.nxt is None:
        return False
    next=n.nxt
    n.val=next.val
    n.nxt=next.nxt
    prnt(n8)
    return True

n2 = Node(7,None)
n3 = Node(6,n2)
n4 = Node(5,n3)
n5 = Node(4,n4)
n6 = Node(3,n5)
n7 = Node(2,n6)
n8 = Node(1,n7)

delete_node(n5)

No comments:

My Profile

My photo
can be reached at 09916017317