Guest User

Untitled

a guest
Dec 15th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. class Node:
  2. def __init__(self,val,next_node=None):
  3. self.val=val
  4. self.next_node = next_node
  5.  
  6. def remove_all_n(head,n):
  7. _head = head
  8. while _head and _head.val == n:
  9. _head = _head.next_node
  10.  
  11. if _head:
  12. _second = _head.next_node
  13.  
  14. head = _head
  15.  
  16. while _second:
  17. if _second.val == n:
  18. if _second.next_node:
  19. _head.next_node = _second.next_node
  20. _second = _second.next_node
  21. else:
  22. _head.next_node = _second.next_node
  23. _second = None
  24. else:
  25. _head = _second
  26. _second = _second.next_node
  27.  
  28. return head
  29.  
  30.  
  31.  
  32.  
  33. head=Node(3,Node(3,Node(3,Node(1,Node(3,Node(3,Node(3,Node(3,Node(3)))))))))
  34. head = remove_all_n(None,3)
  35.  
  36. while head.next_node:
  37. print (head.val)
  38. head = head.next_node
Add Comment
Please, Sign In to add comment