Skip to content

Instantly share code, notes, and snippets.

@yokiy
Created June 26, 2014 01:39
Show Gist options
  • Save yokiy/078e7b4865929a15fa2d to your computer and use it in GitHub Desktop.
Save yokiy/078e7b4865929a15fa2d to your computer and use it in GitHub Desktop.
CC 2.1
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.end = None
def isEmpty(self):
if self.head is None:
return True
else:
return False
def AddNode(self, data):
new = Node(data)
if self.isEmpty():
self.head = new
self.end = new
else:
self.end.next = new;
self.end = self.end.next;
def printList(self):
node = self.head
while node != None:
print node.data
node = node.next
def removedup(self):
hashset = set()
cur = self.head
while (cur.next != None):
# if (cur.data not in hashset):
hashset.add(cur.data)
cur = cur.next
print hashset
def removedupNohash(self):
listcp = self
cur = listcp.head
while cur.next is not None:
running = cur
while running.next is not None:
if running.next.data == cur.data:
running.next = running.next.next
else:
running = running.next
cur = cur.next
print 'after remove:'
listcp.printList()
List = LinkedList()
List.AddNode(1)
List.AddNode(2)
List.AddNode(3)
List.AddNode(2)
List.AddNode(5)
List.AddNode(3)
print 'the list is:',
List.printList()
print 'after remove duplicates:'
List.removedup()
List.removedupNohash()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment