Given an unsorted linked list of N nodes. The task is to remove duplicate elements from this unsorted Linked List. When a value appears in multiple nodes, the node which appeared first should be kept, all others duplicates are to be removed.
Example 1:
Input:
N = 4
value[] = {5,2,2,4}
Output: 5 2 4
Explanation:Given linked list elements are
5->2->2->4, in which 2 is repeated only.
So, we will delete the extra repeated
elements 2 from the linked list and the
resultant linked list will contain 5->2->4
Example 2:
Input:
N = 5
value[] = {2,2,2,2,2}
Output: 2
Explanation:Given linked list elements are
2->2->2->2->2, in which 2 is repeated. So,
we will delete the extra repeated elements
2 from the linked list and the resultant
linked list will contain only 2.
Your Task:
You have to complete the method removeDuplicates() which takes 1 argument: the head of the linked list.  Your function should return a pointer to a linked list with no duplicate element.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= size of linked lists <= 10^{6}
0 <= numbers in list <= 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3
'''
		# Node Class
		class Node:
		    def __init__(self, data):   # data -> value stored in node
		        self.data = data
		        self.next = None
	
'''
class Solution:
    #Function to remove duplicates from unsorted linked list.
    def removeDuplicates(self, head):
        # code here
        # return head after editing list


Solutions:
class Solution:

	def removeDuplicates(self, head):
		if head is None:
			return None
		a = set()
		i = head
		a.add(i.data)
		while i.next:
			if i.next.data not in a:
				a.add(i.next.data)
				i = i.next
			else:
				i.next = i.next.next
		return head


class Solution:

	def removeDuplicates(self, head):
		ptr = head
		m = {}
		m[ptr.data] = 1
		while ptr.next:
			if ptr.next.data in m:
				ptr.next = ptr.next.next
			else:
				m[ptr.next.data] = 1
				ptr = ptr.next
		return head


class Node:

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

class Solution:

	def removeDuplicates(self, head):
		t = head
		m = {}
		m[t.data] = 1
		while t.next:
			if t.next.data in m:
				t.next = t.next.next
			else:
				m[t.next.data] = 1
				t = t.next
		return head


class Solution:

	def removeDuplicates(self, head):
		curr = head
		prev = None
		seen = set()
		if curr == None:
			return head
		else:
			while curr:
				if curr.data not in seen:
					seen.add(curr.data)
					prev = curr
					curr = curr.next
				else:
					prev.next = curr.next
					curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		hash = set()
		curr = head
		hash.add(curr.data)
		while curr.next != None:
			if curr.next.data in hash:
				curr.next = curr.next.next
			else:
				hash.add(curr.next.data)
				curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		if head is None or head.next is None:
			return head
		hash = set()
		current = head
		hash.add(head.data)
		while current.next is not None:
			if current.next.data in hash:
				current.next = current.next.next
			else:
				hash.add(current.next.data)
				current = current.next
		return head


class Solution:

	def removeDuplicates(self, head):
		seen = {}
		curr = head
		prev = head
		if head == None:
			return -1
		while curr != None:
			if curr.data in seen:
				prev.next = curr.next
				curr = curr.next
			else:
				seen[curr.data] = 1
				prev = curr
				curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		if not head.next:
			return head
		d = {head.data: 1}
		temp = head.next
		pre = head
		while temp:
			if temp.data in d:
				temp = temp.next
				continue
			pre.next = temp
			d[temp.data] = 1
			pre = temp
			temp = temp.next
		pre.next = None
		return head


from collections import OrderedDict

class Solution:

	def removeDuplicates(self, head):
		temp = head
		a = []
		while temp:
			a.append(temp.data)
			temp = temp.next
		a = list(OrderedDict.fromkeys(a))
		b = LinkedList()
		for j in a:
			b.append(j)
		return b.head


class Solution:

	def removeDuplicates(self, head):
		itr = head
		s = set()
		while itr and itr.next:
			s.add(itr.data)
			while itr.next and itr.next.data in s:
				itr.next = itr.next.next
			itr = itr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		prev = None
		front = head
		hash = {}
		while front is not None:
			if hash.get(front.data) is not None:
				prev.next = front.next
				front = front.next
			else:
				hash[front.data] = True
				prev = front
				front = front.next
		return head


class Solution:

	def removeDuplicates(self, head):
		currentNode = head
		previousNode = None
		node_set = set()
		while currentNode is not None:
			if currentNode.data in node_set:
				previousNode.next = currentNode.next
			else:
				node_set.add(currentNode.data)
				previousNode = currentNode
			currentNode = currentNode.next
		return head


class Solution:

	def removeDuplicates(self, head):
		m = {}
		c = head
		while c:
			if c.data not in m.keys():
				p = c
				m[c.data] = 1
			if c.data in m.keys():
				p.next = c.next
			c = p.next
		return head


class Solution:

	def removeDuplicates(self, head):
		if head is None or head.next is None:
			return head
		seen = set()
		prev = head
		seen.add(head.data)
		ptr = head.next
		while ptr:
			if ptr.data in seen:
				prev.next = ptr.next
				ptr = ptr.next
				continue
			else:
				seen.add(ptr.data)
				prev = ptr
				ptr = ptr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		nodes = set()
		nodes.add(head.data)
		temp = head
		while temp.next:
			if temp.next.data not in nodes:
				nodes.add(temp.next.data)
				temp = temp.next
			else:
				temp.next = temp.next.next
		return head


class Solution:

	def removeDuplicates(self, head):
		if not head or not head.next:
			return head
		hashmap = {}
		pre = None
		curr = head
		while curr:
			nex = curr.next
			if curr.data in hashmap:
				pre.next = nex
				curr.next = None
				curr = nex
			else:
				hashmap[curr.data] = 1
				pre = curr
				curr = nex
		return head


class Solution:

	def removeDuplicates(self, head):
		if head is None:
			return head
		s = set()
		cur = head
		prev = None
		while cur:
			if cur.data in s:
				prev.next = cur.next
				cur = cur.next
			else:
				s.add(cur.data)
				prev = cur
				cur = cur.next
		return head


class Solution:

	def removeDuplicates(self, head):
		h = head
		s = set([h.data])
		while h.next != None:
			if h.next.data in s:
				h.next = h.next.next
			else:
				s.add(h.next.data)
				h = h.next
		return head


class Solution:

	def removeDuplicates(self, head):
		covered = set()
		ptr = head
		covered.add(head.data)
		while ptr.next:
			if ptr.next.data in covered:
				ptr.next = ptr.next.next
			else:
				covered.add(ptr.next.data)
				ptr = ptr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		dic = {}
		lst = []
		temp = head
		while temp:
			if temp.data not in dic:
				lst.append(temp.data)
				dic[temp.data] = 1
			temp = temp.next
		nn = Node(0)
		current = nn
		for i in lst:
			current.next = Node(i)
			current = current.next
		return nn.next


class Solution:

	def removeDuplicates(self, head):
		hashmap = {}
		temp = head
		if temp == None:
			return None
		hashmap[head.data] = head.data
		while temp.next != None:
			if temp.next.data in hashmap:
				temp.next = temp.next.next
			else:
				hashmap[temp.next.data] = temp.next.data
				temp = temp.next
		return head


class Solution:

	def removeDuplicates(self, head):
		m = {}
		if head == None or head.next == None:
			return head
		temp = head.next
		prev = head
		m[head.data] = 1
		while temp != None:
			if temp.data not in m:
				m[temp.data] = 1
				temp = temp.next
				prev = prev.next
			elif temp.data in m:
				temp = temp.next
				prev.next = temp
		return head


class Solution:

	def removeDuplicates(self, head):
		seen = set()
		prev = Node(0)
		curr = head
		prev.next = curr
		n = curr.next
		while curr is not None:
			if curr.data in seen:
				prev.next = curr.next
				curr = curr.next
			else:
				seen.add(curr.data)
				prev = prev.next
				curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		dicti_data = {}
		currentele = head
		dicti_data[currentele.data] = 1
		while currentele and currentele.next:
			if currentele.next.data in dicti_data:
				currentele.next = currentele.next.next
			else:
				dicti_data[currentele.next.data] = 1
				currentele = currentele.next
		return head


class Solution:

	def removeDuplicates(self, head):
		de = set()
		prev = None
		temp = head
		while temp is not None:
			if temp.data not in de:
				de.add(temp.data)
				prev = temp
			else:
				prev.next = temp.next
				del temp
			temp = prev.next
		return head


class Solution:

	def removeDuplicates(self, head):
		prev = None
		curr = head
		map = {}
		while curr != None:
			map[curr.data] = False
			curr = curr.next
		curr = head
		while curr != None:
			val = curr.data
			if map[val] == True:
				prev.next = curr.next
				del curr
				curr = prev.next
			else:
				map[val] = True
				prev = curr
				curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		if head == None:
			return head
		else:
			curr = head
			dict1 = {}
			dict1[curr.data] = True
			temp = curr.next
			while temp != None:
				try:
					if dict1[temp.data]:
						temp = temp.next
				except:
					dict1[temp.data] = True
					curr.next = temp
					curr = temp
					temp = temp.next
			curr.next = None
			return head


class Solution:

	def removeDuplicates(self, head):
		dummy = Node(0)
		tail = dummy
		hashSet = set()
		while head:
			if head.data not in hashSet:
				tail.next = head
				tail = tail.next
				hashSet.add(head.data)
			head = head.next
		tail.next = None
		return dummy.next


class Solution:

	def removeDuplicates(self, head):
		s = set()
		if head == None or head.next == None:
			return head
		cur = head
		dummy = Node(-1)
		dummy.next = head
		temp = dummy
		while cur:
			if cur.data not in s:
				s.add(cur.data)
				temp.next = cur
				temp = cur
				cur = cur.next
			else:
				cur = cur.next
		temp.next = cur
		return dummy.next


from collections import OrderedDict

class Solution:

	def removeDuplicates(self, head):
		L = []
		n = head
		while n is not None:
			L.append(n.data)
			n = n.next
		L = list(OrderedDict.fromkeys(L))
		n = head
		while L:
			n.data = L.pop(0)
			a = n
			n = n.next
		a.next = None
		return head


class Solution:

	def removeDuplicates(self, head):
		mpp = {}
		temp = head
		while temp != None and temp.next != None:
			mpp[temp.data] = True
			if temp.next.data in mpp:
				toBeDeleted = temp.next
				temp.next = toBeDeleted.next
				toBeDeleted = None
			else:
				temp = temp.next
		return head


class Solution:

	def removeDuplicates(self, head):
		m = set()
		current = head
		m.add(head.data)
		while current.next != None:
			if current.next.data in m:
				current.next = current.next.next
			else:
				m.add(current.next.data)
				current = current.next
		return head


class Solution:

	def removeDuplicates(self, head):
		if not head or not head.next:
			return head
		seen = set()
		seen.add(head.data)
		current = head
		while current.next:
			if current.next.data in seen:
				next_node = current.next
				current.next = next_node.next
				next_node.next = None
				del next_node
			else:
				seen.add(current.next.data)
				current = current.next
		return head


class Solution:

	def removeDuplicates(self, head):
		c = head
		p = None
		d = {}
		while c:
			if c.data in d:
				p.next = c.next
				c = None
			else:
				d[c.data] = 1
				p = c
			c = p.next
		return head


class Solution:

	def removeDuplicates(self, head):
		node_map = {head.data: 1}
		temp = head
		while temp:
			temp2 = temp.next
			while temp2 and node_map.get(temp2.data):
				temp2 = temp2.next
			temp.next = temp2
			if temp2:
				node_map[temp2.data] = 1
			temp = temp.next
		return head


class Solution:

	def removeDuplicates(self, head):
		d = {}
		curr = head
		d[head.data] = True
		while curr.next is not None:
			if curr.next.data in d:
				curr.next = curr.next.next
			else:
				d[curr.next.data] = True
				curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		lst = []
		se = set()
		curr = head
		while curr:
			if curr.data not in se:
				se.add(curr.data)
				lst.append(curr.data)
			curr = curr.next
		curr = head
		prev = None
		while lst:
			prev = curr
			curr.data = lst.pop(0)
			curr = curr.next
		prev.next = None
		return head


class Solution:

	def removeDuplicates(self, head):
		if head == None:
			return None
		if head.next == None:
			return head
		newhead = head
		l = set()
		prev = None
		temp = head
		while temp != None:
			if temp.data not in l:
				l.add(temp.data)
				prev = temp
				temp = temp.next
			else:
				temp = temp.next
				prev.next = temp
		return newhead


class Solution:

	def removeDuplicates(self, head):
		seen = set()
		c = head
		last = None
		while c:
			if c.data in seen:
				last.next = c.next
			else:
				seen.add(c.data)
				last = c
			c = c.next
		return head


class Solution:

	def removeDuplicates(self, head):
		_d = {}
		temp = head
		_prev = head
		while temp:
			try:
				_ = _d[temp.data]
				_prev.next = temp.next
			except Exception as e:
				_prev = temp
				_d[temp.data] = -1
			temp = temp.next
		return head


class Solution:

	def removeDuplicates(self, head):
		if not head or not head.next:
			return head
		s = set()
		itr = head
		s.add(itr.data)
		while itr.next:
			if itr.next.data not in s:
				s.add(itr.next.data)
				itr = itr.next
			else:
				itr.next = itr.next.next
			if not itr:
				break
		return head


class Solution:

	def removeDuplicates(self, head):
		if head == None and head.next == None:
			return head
		temp = Node(-1)
		curr = temp
		d = set()
		while head:
			if head.data not in d:
				curr.next = Node(head.data)
				curr = curr.next
			d.add(head.data)
			head = head.next
		return temp.next


class Solution:

	def removeDuplicates(self, head):
		temp = head
		prev = None
		reps = set()
		while temp:
			if temp.data in reps:
				prev.next = temp.next
			else:
				reps.add(temp.data)
				prev = temp
			temp = temp.next
		return head


class Solution:

	def removeDuplicates(self, head):
		ref = {}
		current = head
		previous = None
		while current != None:
			if current.data in ref:
				previous.next = current.next
				current = current.next
			else:
				ref[current.data] = 1
				previous = current
				current = current.next
		return head


class Solution:

	def removeDuplicates(self, head):
		_set = set()
		newhead = head
		temp = newhead
		_set.add(head.data)
		while head is not None:
			if head.data not in _set:
				_set.add(head.data)
				temp.next = head
				temp = temp.next
			head = head.next
		temp.next = None
		return newhead


class Solution:

	def removeDuplicates(self, head):
		s = set()
		current = head
		while current.next:
			s.add(current.data)
			if current.next.data in s:
				current.next = current.next.next
			else:
				current = current.next
		return head


class Solution:

	def removeDuplicates(self, head):
		curr = head
		d = set()
		d.add(head.data)
		while curr and curr.next:
			if curr.next.data in d:
				curr.next = curr.next.next
			else:
				d.add(curr.next.data)
				curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		seen = set()
		dummy = Node(0)
		dummy.next = head
		(prev, curr) = (dummy, head)
		while curr:
			if curr.data in seen:
				prev.next = curr.next
			else:
				seen.add(curr.data)
				prev = curr
			curr = curr.next
		return dummy.next


class Solution:

	def removeDuplicates(self, head):
		prev = None
		curr = head
		mp = {}
		while curr != None:
			val = curr.data
			if val in mp:
				prev.next = curr.next
				del curr
				curr = prev.next
			else:
				mp[val] = True
				prev = curr
				curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		node = head
		prev = None
		dic = {}
		while node:
			if node.data not in dic:
				dic[node.data] = 1
				prev = node
			else:
				prev.next = node.next
			node = node.next
		return head


class Solution:

	def removeDuplicates(self, head):
		(numbers, tmp, previous) = (set(), head, Node(0))
		previous.next = tmp
		while tmp is not None:
			while tmp is not None and numbers.__contains__(tmp.data):
				tmp = tmp.next
			previous.next = tmp
			previous = tmp
			if tmp is not None:
				numbers.add(tmp.data)
				tmp = tmp.next
		return head


class Solution:

	def removeDuplicates(self, head):
		d = {}
		d[head.data] = True
		temp = head
		while temp.next:
			if temp.next.data in d:
				temp.next = temp.next.next
			else:
				d[temp.next.data] = True
				temp = temp.next
			if temp == None:
				return head
		return head


class Solution:

	def removeDuplicates(self, head):
		if head == None:
			return None
		d = {head.data: 1}
		curr = head.next
		prev = head
		while curr is not None:
			if curr.data in d:
				a = curr.next
				del curr
				curr = a
				prev.next = curr
			else:
				d[curr.data] = 1
				prev = curr
				curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		visited = {}
		tail = head
		prev = head
		while tail:
			if tail.data not in visited:
				visited[tail.data] = tail.data
				prev = tail
				tail = tail.next
			else:
				prev.next = tail.next
				tail = prev
				tail = tail.next
		return head


class Solution:

	def removeDuplicates(self, head):
		hash = set()
		curr = head
		pre = None
		while curr is not None:
			if curr.data in hash:
				pre.next = curr.next
				del curr
			else:
				hash.add(curr.data)
				pre = curr
			curr = pre.next
		return head


class Solution:

	def removeDuplicates(self, head):
		d = {}
		cur = head
		prev = None
		while cur:
			if cur.data in d:
				prev.next = cur.next
				cur = cur.next
			else:
				d[cur.data] = 1
				prev = cur
				cur = cur.next
		return head


class Solution:

	def removeDuplicates(self, head):
		array = set()
		dummy = Node(-1)
		temp = dummy
		curr = head
		while curr:
			if curr.data not in array:
				array.add(curr.data)
				newnode = Node(curr.data)
				temp.next = newnode
				temp = temp.next
			curr = curr.next
		return dummy.next


class Solution:

	def removeDuplicates(self, head):
		existing = dict()
		prev = None
		curr = head
		while curr is not None:
			if curr.data in existing.keys():
				prev.next = curr.next
				prev = prev
				curr = curr.next
			else:
				existing[curr.data] = 1
				prev = curr
				curr = curr.next
		return head


class Solution:

	def removeDuplicates(self, head):
		curr = head
		nxt = curr.next
		mp = {}
		mp[curr.data] = 1
		while nxt != None:
			if nxt.data in mp:
				nxt = nxt.next
				curr.next = nxt
			else:
				mp[nxt.data] = 1
				curr = nxt
				nxt = nxt.next
		return head