Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list. 
Example 1:
Input: 
N = 8
Linked List = 23->28->28->35->49->49->53->53
Output: 
23 35
Explanation:
The duplicate numbers are 28, 49 and 53 which 
are removed from the list.
Example 2:
Input:
N = 6
Linked List = 11->11->11->11->75->75
Output: 
Empty list
Explanation:
All the nodes in the linked list have 
duplicates. Hence the resultant list 
would be empty.
Your task:
You don't have to read input or print anything. Your task is to complete the function removeAllDuplicates() which takes the head of the linked list, removes all the occurences of duplicates in the linked list and returns the head of the modified linked list.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 10^5

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Data structures

Starter code: #User function Template for python3

"""
# Linked list Node class

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

"""

class Solution:
    def removeAllDuplicates(self, head):
        #code here

Solutions:
class Solution:

	def removeAllDuplicates(self, head):
		if head == None:
			return head
		if head.next == None:
			return head
		newl = Node(-69)
		newl.next = head
		temp = newl

		def fn(node):
			while True:
				try:
					if node.data == node.next.data:
						node = node.next
					else:
						return node.next
				except:
					return None

		def printl(node):
			while True:
				try:
					node = node.next
				except:
					return
		while True:
			try:
				if temp.next.data == temp.next.next.data:
					temp.next = fn(temp.next)
				else:
					temp = temp.next
			except:
				return newl.next


class Solution:

	def removeAllDuplicates(self, head):
		d = {}
		temp = head
		while temp:
			if temp.data not in d:
				d[temp.data] = 1
			else:
				d[temp.data] += 1
			temp = temp.next
		temp = head
		prev = Node(-1)
		dummy = prev
		while temp:
			if d[temp.data] == 1:
				prev.next = temp
				prev = prev.next
			temp = temp.next
		prev.next = None
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		l = []
		dic = {}
		a = head
		while a:
			if a.data in dic:
				dic[a.data] += 1
			else:
				dic[a.data] = 1
			a = a.next
		prev = Node(-1)
		dummy = prev
		a = head
		while a:
			if dic[a.data] == 1:
				prev.next = a
				prev = prev.next
			a = a.next
		prev.next = None
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		d = Node(-1)
		ite = d
		ite.next = head
		curr = head.next
		while curr:
			flag = False
			while curr and ite.next.data == curr.data:
				curr = curr.next
				flag = True
			if flag:
				ite.next = curr
				if curr:
					curr = curr.next
			else:
				ite = ite.next
				curr = curr.next
		return d.next


class Solution:

	def removeAllDuplicates(self, head):
		s = head
		sol = []
		l = {}
		ans = []
		while s:
			k = s.data
			l[k] = l.get(k, 0) + 1
			sol.append(k)
			s = s.next
		for i in sol:
			if l[i] == 1:
				ans.append(i)
		start = None
		curr = None
		while ans:
			if not start:
				start = Node(ans.pop(0))
				curr = start
			else:
				curr.next = Node(ans.pop(0))
				curr = curr.next
		return start


class Solution:

	def removeAllDuplicates(self, head):
		llarr = []
		m = {}
		h = head
		while h:
			if h.data in m.keys():
				m[h.data] += 1
			else:
				m[h.data] = 1
			h = h.next
		for i in m:
			if m[i] == 1:
				llarr.append(i)
		dummy = Node(-1)
		head = dummy
		llarr.sort()
		for i in llarr:
			node = Node(i)
			head.next = node
			head = node
		return dummy.next


from collections import Counter

class Solution:

	def removeAllDuplicates(self, head):
		n = head
		L = []
		while n is not None:
			L.append(n.data)
			n = n.next
		c = Counter(L)
		x = []
		for i in c:
			if c[i] == 1:
				x.append(i)
		if len(L) == len(x):
			return head
		x.sort()
		if not x:
			return None
		n = head
		while x:
			n.data = x.pop(0)
			a = n
			n = n.next
		a.next = None
		return head


class Solution:

	def removeAllDuplicates(self, head):
		newhead = Node(2 ** 31)
		prev = newhead
		prev_prev = None
		dup = False
		while head:
			if head.data != prev.data:
				if dup:
					prev_prev.next = head
					prev = head
				else:
					prev_prev = prev
					prev.next = head
					prev = head
				dup = False
			else:
				prev_prev.next = head
				prev = head
				dup = True
			head = head.next
		if dup:
			prev_prev.next = head
		return newhead.next


class Solution:

	def removeAllDuplicates(self, head):
		node = head
		prev = Node(-1)
		head = prev
		while node and node.next:
			if node.data == node.next.data:
				data = node.data
				while node != None and data == node.data:
					node = node.next
			else:
				prev.next = node
				prev = prev.next
				node = node.next
		prev.next = node
		return head.next


import collections

class Solution:

	def removeAllDuplicates(self, head):
		p = []
		a = head
		while a is not None:
			p.append(a.data)
			a = a.next
		dict1 = collections.Counter(p)
		q = []
		b = None
		head1 = None
		for i in p:
			if dict1[i] == 1:
				if i not in q:
					m = Node(i)
					q.append(i)
					if head1 == None:
						head1 = m
						b = head1
					else:
						b.next = m
						b = b.next
		return head1


class Solution:

	def removeAllDuplicates(self, head):
		cur = head
		start = None
		while cur != None:
			v = cur.data
			prev = cur
			count = 0
			while cur.next != None and cur.next.data == v:
				count += 1
				cur = cur.next
			if count == 0:
				if start == None:
					start = prev
					head = start
				else:
					start.next = prev
					start = start.next
			cur = cur.next
		start.next = None
		return head


class Solution:

	def removeAllDuplicates(self, head):
		flag1 = False
		while head and head.next and (head.data == head.next.data):
			flag1 = True
			head = head.next
		temp = head.next
		pre = head
		while temp:
			flag = False
			while temp and temp.next and (temp.data == temp.next.data):
				flag = True
				temp.next = temp.next.next
			if flag:
				pre.next = temp.next
				temp = temp.next
			else:
				pre = temp
				temp = temp.next
		if flag1:
			head = head.next
		return head


class Solution:

	def removeAllDuplicates(self, head):
		lst = []
		ptr = head
		visited = set()
		while ptr:
			if ptr.data in visited:
				try:
					lst.remove(ptr.data)
				except Exception as e:
					ptr = ptr.next
			else:
				lst.append(ptr.data)
				visited.add(ptr.data)
				ptr = ptr.next
		ll = LinkedList()
		for i in lst:
			ll.append(i)
		return ll.head


from collections import Counter

class Node:

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

class Solution:

	def removeAllDuplicates(self, head):
		l = []
		n = head
		while n is not None:
			l += [n.data]
			n = n.next
		d = Counter(l)
		l = [i for i in d if d[i] == 1]
		l = sorted(l)
		if l == []:
			return None
		head = None
		head = Node(l[0])
		n = head
		if len(l) == 1:
			return head
		else:
			for i in range(1, len(l)):
				no = Node(l[i])
				no.next = None
				n.next = no
				n = n.next
			return head


class Solution:

	def removeAllDuplicates(self, head):
		dummy = Node(-1)
		temp = dummy
		temp.next = head
		curr = head
		while curr:
			while curr and curr.next and (curr.data == curr.next.data):
				curr = curr.next
			if temp.next != curr:
				temp.next = curr.next
			else:
				temp = temp.next
			curr = curr.next
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		dummy = Node(0)
		dummy.next = head
		prev = dummy
		curr = head
		while curr and curr.next:
			while curr.next and curr.data == curr.next.data:
				curr = curr.next
			if prev.next == curr:
				prev = prev.next
			else:
				prev.next = curr.next
			curr = curr.next
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		p = q = head
		while p and p.next:
			if p == head:
				if p.data == p.next.data:
					t = p
					while t.next and t.data == t.next.data:
						t = t.next
					head = t.next
					t.next = None
					p = q = head
				else:
					q = p
					p = p.next
			elif p.data == p.next.data:
				t = p
				while t.next and t.data == t.next.data:
					t = t.next
				q.next = t.next
				t.next = None
				p = q.next
			else:
				q = p
				p = p.next
		return head


class Solution:

	def removeAllDuplicates(self, head):
		dic = {}
		b = []
		tmp = head
		while tmp:
			if tmp.data in dic:
				dic[tmp.data] += 1
			else:
				dic[tmp.data] = 1
			b.append(tmp.data)
			tmp = tmp.next
		tmp = head
		prev = tmp
		for i in range(len(b)):
			if dic[b[i]] == 1:
				tmp.data = b[i]
				if i == len(b) - 1:
					tmp.next = None
					return head
				prev = tmp
				tmp = tmp.next
			if i == len(b) - 1:
				prev.next = None
		return head


class Solution:

	def removeAllDuplicates(self, head):
		slow = head
		ptr = head
		z = []
		while slow is not None:
			z.append(slow.data)
			slow = slow.next
		d = dict()
		for ele in z:
			d[ele] = 0
		for ele in z:
			d[ele] = d[ele] + 1
		k = []
		for ele in z:
			if d[ele] == 1:
				k.append(ele)
		k.append('x')
		head = None
		tail = None
		for ele in k:
			if ele != 'x':
				if head is None:
					newnode = Node(ele)
					head = newnode
					tail = newnode
				else:
					newnode = Node(ele)
					tail.next = newnode
					tail = newnode
		return head


class Solution:

	def removeAllDuplicates(self, head):
		(x, r) = ([], [])
		while head:
			x.append(head.data)
			head = head.next
		d = {}
		for i in x:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in d:
			if d[i] == 1:
				r.append(i)
		r.sort()
		a = b = Node(0)
		for i in r:
			a.next = Node(i)
			a = a.next
		return b.next


from collections import Counter

class Solution:

	def removeAllDuplicates(self, head):
		s = []
		while head != None:
			s.append(head.data)
			head = head.next
		t = Counter(s)
		v = []
		for j in t:
			if t[j] == 1:
				v.append(j)
		v = sorted(v)
		q = Node(v[0])
		w = q
		for j in range(1, len(v)):
			r = Node(v[j])
			q.next = r
			q = q.next
		return w


class Solution:

	def removeAllDuplicates(self, head):
		if head is None or head.next is None:
			return head
		dummy = Node(-1)
		iterator = dummy
		iterator.next = head
		current = head.next
		while current:
			flag = False
			while current and iterator.next.data == current.data:
				flag = True
				current = current.next
			if flag:
				iterator.next = current
			else:
				iterator = iterator.next
			if current:
				current = current.next
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		d = {}
		temp = head
		while temp:
			if temp.data in d:
				d[temp.data] += 1
			else:
				d[temp.data] = 1
			temp = temp.next
		dum = Node(-1)
		temp = dum
		curr = head
		while curr:
			if d[curr.data] == 1:
				temp.next = curr
				temp = temp.next
			curr = curr.next
			temp.next = None
		return dum.next


class Solution:

	def removeAllDuplicates(self, head):
		temp = head
		ans = val = Node(-1)
		duplicate = False
		while temp.next:
			if temp.data != temp.next.data:
				if not duplicate:
					val.next = Node(temp.data)
					val = val.next
				duplicate = False
				temp = temp.next
			else:
				temp = temp.next
				duplicate = True
		if not duplicate:
			val.next = Node(temp.data)
			val = val.next
		return ans.next


class Solution:

	def removeAllDuplicates(self, head):
		dummy = Node
		dummy.next = head
		ptr = dummy
		while head and head.next:
			if head.next and head.data == head.next.data:
				while head.next and head.data == head.next.data:
					head = head.next
				ptr.next = head.next
			else:
				ptr = ptr.next
			head = head.next
		return dummy.next


from collections import OrderedDict

class Solution:

	def removeAllDuplicates(self, head):
		tek = head
		dikt = dict()
		while tek != None:
			dikt[tek.data] = 0
			tek = tek.next
		tek = head
		while tek != None:
			dikt[tek.data] += 1
			tek = tek.next
		dikt1 = OrderedDict(sorted(dikt.items()))
		lk = list(dikt1.keys())
		lv = list(dikt1.values())
		head3 = Node(0)
		tek = head3
		i = 0
		while i < len(lv):
			if lv[i] == 1:
				tek.next = Node(lk[i])
				tek = tek.next
			i += 1
		return head3.next


class Solution:

	def removeAllDuplicates(self, head):
		visit = set()
		ref = set()
		curr = head
		while curr:
			if curr.data not in visit:
				visit.add(curr.data)
				curr = curr.next
			else:
				ref.add(curr.data)
				curr = curr.next
		tmp = head
		while tmp.next:
			if tmp.next.data in ref:
				tmp.next = tmp.next.next
			else:
				tmp = tmp.next
		if head.data in ref:
			y = head.next
			head.next = None
			head = y
		return head
		return head


class Solution:

	def removeAllDuplicates(self, head):
		cop = head

		def remove(curr):
			if curr.next:
				curr.data = curr.next.data
				curr.next = curr.next.next
			else:
				curr.data = None
		visited = set()
		while head and head.next:
			if head.data in visited:
				remove(head)
				continue
			visited.add(head.data)
			if head.data == head.next.data:
				remove(head)
				continue
			head = head.next
		if head.data in visited:
			remove(head)
		head = cop
		while cop and cop.next:
			if cop.next.data == None:
				cop.next = None
			cop = cop.next
		return head


class Solution:

	def removeAllDuplicates(self, head):
		dummy = pre = Node(0)
		dummy.next = head
		while head and head.next:
			if head.data == head.next.data:
				while head and head.next and (head.data == head.next.data):
					head = head.next
				head = head.next
				pre.next = head
			else:
				pre = pre.next
				head = head.next
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		arr = {}
		curr = head
		while curr:
			x = curr.data
			if x in arr:
				arr[x] += 1
			else:
				arr[x] = 1
			curr = curr.next
		new = Node(-1)
		curr = new
		for i in sorted(arr):
			if arr[i] == 1:
				curr.next = Node(i)
				curr = curr.next
		return new.next


class Solution:

	def removeAllDuplicates(self, head):
		if head is None or head.next is None:
			return head
		dummy = Node(0)
		dummy.next = head
		prev = dummy
		while head and head.next:
			if head.data != head.next.data:
				prev = head
				head = head.next
			else:
				while head.next and head.data == head.next.data:
					head = head.next
				prev.next = head.next
				head = head.next
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		curr = head
		prev = temp = Node(None)
		temp.next = curr
		while curr and curr.next:
			if curr.data == curr.next.data:
				while curr and curr.next and (curr.data == curr.next.data):
					curr = curr.next
				curr = curr.next
				prev.next = curr
			else:
				prev = prev.next
				curr = curr.next
		return temp.next


class Node:

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

class Solution:

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

	def removeAllDuplicates(self, head):
		tempNode = head
		d = {}
		while tempNode != None:
			if tempNode.data not in d:
				d[tempNode.data] = 1
			else:
				d[tempNode.data] += 1
			tempNode = tempNode.next
		ans = []
		for i in d:
			if d[i] == 1:
				ans.append(i)
		ans = sorted(ans)
		for i in ans:
			newNode = Node(i)
			if self.head == None:
				self.head = newNode
				self.tail = newNode
			else:
				self.tail.next = newNode
				self.tail = newNode
		return self.head


import collections
import itertools

class Solution:

	def removeAllDuplicates(self, head):
		dic = collections.defaultdict(int)
		temp = head
		while head:
			dic[head.data] += 1
			head = head.next
		a = Node(0)
		temp = a
		for i in sorted(dic.keys()):
			if dic[i] == 1:
				temp.next = Node(i)
				temp = temp.next
		return a.next


class Solution:

	def removeAllDuplicates(self, head):
		d = {}
		temp = head
		while temp:
			if temp.data not in d:
				d[temp.data] = 1
			else:
				d[temp.data] += 1
			temp = temp.next
		while d[head.data] > 1:
			head = head.next
		if not head or not head.next:
			return head
		prev = head
		temp = head.next
		while temp:
			if d[temp.data] > 1:
				prev.next = temp.next
				temp = temp.next
			else:
				prev = temp
				temp = temp.next
		return head


class Solution:

	def removeAllDuplicates(self, head):
		dic = {}
		itr = head
		while itr:
			if itr.data in dic:
				dic[itr.data] += 1
			else:
				dic[itr.data] = 1
			itr = itr.next
		ans = pre = Node(0)
		itr = head
		while itr:
			if dic[itr.data] == 1:
				pre.next = itr
				itr = itr.next
				pre = pre.next
				pre.next = None
			else:
				itr = itr.next
		return ans.next


class Solution:

	def removeAllDuplicates(self, head):
		a = []
		b = head
		while b:
			a.append(b.data)
			b = b.next
		c = []
		g = dict.fromkeys(a, 0)
		for i in a:
			g[i] += 1
		for j in a:
			if g[j] == 1:
				c.append(j)
		t1 = h1 = Node(c[0])
		for i in range(1, len(c)):
			x = Node(c[i])
			t1.next = x
			t1 = t1.next
		return h1


class Solution:

	def removeAllDuplicates(self, head):
		start_next = False
		while head.next and head.data == head.next.data:
			head = head.next
			start_next = True
		if not head:
			return None
		start = head
		remove = False
		while head.next and head.next.next:
			while head.next.next and head.next.data == head.next.next.data:
				remove = True
				head.next = head.next.next
			if remove:
				head.next = head.next.next
				remove = False
			else:
				head = head.next
		return start.next if start_next else start


class Solution:

	def removeAllDuplicates(self, head):
		ch = head
		li = []
		while head != None:
			li.append(head.data)
			head = head.next
		dena_head = ch
		d = {}
		for x in li:
			if d.get(x) == None:
				d[x] = 1
			else:
				d[x] += 1
		li = [x for x in d if d[x] == 1]
		li.sort()
		ch.data = li[0]
		for x in li[1:]:
			ch.next = Node(x)
			ch = ch.next
		ch.next = None
		return dena_head


class Solution:

	def removeAllDuplicates(self, head):
		freq = {}
		temp = head
		while temp != None:
			freq[temp.data] = 0
			temp = temp.next
		temp = head
		while temp != None:
			freq[temp.data] += 1
			temp = temp.next
		head2 = None
		temp = head
		while temp != None:
			if freq[temp.data] > 1:
				temp = temp.next
			elif head2 == None:
				head2 = Node(temp.data)
				temp2 = head2
				temp = temp.next
			else:
				temp2.next = Node(temp.data)
				temp2 = temp2.next
				temp = temp.next
		return head2


class Solution:

	def removeAllDuplicates(self, head):
		pre = None
		node = head
		nhead = None
		k = 0
		while node:
			c = 0
			val = node.data
			while node.next and node.next.data == val:
				node = node.next
				c = 1
			if not nhead and c == 0 and node:
				nhead = node
			if c == 1:
				if pre:
					pre.next = node.next
					node = node.next
				else:
					temp = node
					node = node.next
					temp.next = None
			else:
				pre = node
				node = node.next
		head = nhead
		return head


from collections import Counter

class Solution:

	def removeAllDuplicates(self, head):
		l = []
		l1 = []
		curr = head
		while curr is not None:
			l.append(curr.data)
			curr = curr.next
		c = Counter(l)
		for (i, j) in c.items():
			if j == 1:
				l1.append(i)
		l1.sort()
		if len(l1) > 0:
			for i in l1:
				print(i, end=' ')
		else:
			print('Empty list')


class Solution:

	def removeAllDuplicates(self, head):
		dummy = Node(-1)
		dNode = dummy
		node = head
		while node:
			found = False
			while node and node.next:
				if node.data == node.next.data:
					found = True
					node = node.next
				else:
					break
			if not found:
				dNode.next = Node(node.data)
				dNode = dNode.next
			node = node.next
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		a = {}
		cur = head
		while cur is not None:
			if cur.data not in a:
				a[cur.data] = 1
			else:
				a[cur.data] += 1
			cur = cur.next
		b = []
		for i in a:
			if a[i] == 1:
				b.append(i)
		b.sort()
		for i in b:
			print(i, end=' ')


class Solution:

	def removeAllDuplicates(self, head):
		ne_head = Node(0)
		t = ne_head
		a = head
		b = head.next
		while a:
			while b and b.data == a.data:
				b = b.next
			if a.next == b:
				t.next = a
				a = b
				if b:
					b = b.next
				t = t.next
			else:
				a = b
				if a:
					b = b.next
		t.next = None
		return ne_head.next


class Solution:

	def removeAllDuplicates(self, head):
		temp = head
		(K, L) = ({}, [])
		while temp:
			x = temp.data
			if x in K:
				K[x] += 1
			else:
				K[x] = 1
			temp = temp.next
		for i in K:
			if K[i] == 1:
				L.append(i)
		L.sort()
		new = Node(L[0])
		M = LinkedList()
		for i in L:
			M.append(i)
		return M.head


class Solution:

	def removeAllDuplicates(self, head):
		duplicate_list = set()
		curr = head
		while curr and curr.next:
			if curr.data == curr.next.data:
				duplicate_list.add(curr.data)
			curr = curr.next
		dummy = Node(0)
		dummy.next = head
		curr = dummy
		while curr and curr.next:
			if curr.next.data in duplicate_list:
				curr.next = curr.next.next
			else:
				curr = curr.next
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		h = Node(0)
		h.next = head
		temp = head
		prev = h
		while temp != None:
			if temp.next != None and temp.data == temp.next.data:
				t = temp
				while temp != None and temp.data == t.data:
					temp = temp.next
				prev.next = temp
			else:
				prev = temp
				temp = temp.next
		return h.next


class Solution:

	def removeAllDuplicates(self, head):
		s = list()
		x = None
		if head == None:
			return None
		if head.next == None:
			return head
		while head:
			if head.data not in s:
				if head.data != x:
					s.append(head.data)
				head = head.next
				continue
			if head.data in s:
				x = s.pop()
				head = head.next
				continue
		l = LinkedList()
		for i in s:
			l.append(i)
		return l.head


class Solution:

	def removeAllDuplicates(self, head):
		s = list()
		x = None
		if head == None:
			return
		if head.next == None:
			return head
		while head:
			if head.data not in s:
				if head.data != x:
					s.append(head.data)
				head = head.next
				continue
			if head.data in s:
				x = s.pop()
				head = head.next
				continue
		newlist = None
		for i in s:
			if newlist == None:
				newlist = Node(i)
				temp = newlist
			else:
				temp.next = Node(i)
				temp = temp.next
		return newlist


from collections import OrderedDict

class Solution:

	def removeAllDuplicates(self, head):
		k = OrderedDict()
		temp = head
		while temp:
			if temp.data in k.keys():
				k[temp.data] += 1
			else:
				k[temp.data] = 1
			temp = temp.next
		temp2 = head
		ls = []
		for i in k.keys():
			if k[i] == 1:
				ls.append(i)
		a = LinkedList()
		for x in ls:
			a.append(x)
		return a.head


class Solution:

	def removeAllDuplicates(self, head):
		dict1 = {}
		n = head
		while n:
			dict1[n.data] = dict1.get(n.data, 0) + 1
			n = n.next
		ans = None
		for i in sorted(dict1.keys()):
			if dict1[i] == 1:
				if ans == None:
					ans = Node(i)
					prev = ans
				else:
					prev.next = Node(i)
					prev = prev.next
		return ans


class Solution:

	def removeAllDuplicates(self, head):
		if head == None:
			return
		root = Node(0)
		newNode = root
		root.next = head
		temp = head
		while temp != None and temp.next != None:
			if temp.data == temp.next.data:
				while temp != None and temp.next != None and (temp.data == temp.next.data):
					temp = temp.next
				temp = temp.next
				newNode.next = temp
			else:
				newNode = newNode.next
				temp = temp.next
		return root.next


class Solution:

	def removeAllDuplicates(self, head):
		d = Node(-1)
		temp = d
		temp.next = head
		curr = head.next
		while curr != None:
			looprun = False
			while curr and temp.next.data == curr.data:
				curr = curr.next
				looprun = True
			if looprun == True:
				temp.next = curr
			else:
				temp = temp.next
			if curr:
				curr = curr.next
		return d.next


class Solution:

	def removeAllDuplicates(self, head):
		hm = {}
		curr = head
		while curr != None:
			hm[curr.data] = 1 + hm.get(curr.data, 0)
			curr = curr.next
		newh = head
		curr = head
		prev = head
		f = 0
		while curr != None:
			while curr and hm[curr.data] > 1:
				curr = curr.next
			if curr and hm[curr.data] == 1 and (f == 0):
				newh = curr
				prev = curr
				curr = curr.next
				prev.next = None
				f = 1
			elif curr and hm[curr.data] == 1 and (f == 1):
				prev.next = curr
				prev = curr
				curr = curr.next
				prev.next = None
		return newh


class Solution:

	def removeAllDuplicates(self, head):
		dic = {}
		temp = head
		prev_node = Node(-1)
		prev_node.next = head
		head = prev_node
		while temp != None:
			if temp != None and temp.next != None and (temp.data == temp.next.data):
				while temp != None and temp.next != None and (temp.data == temp.next.data):
					temp.next = temp.next.next
				prev_node.next = temp.next
			else:
				prev_node = temp
			temp = temp.next
		return head.next


class Solution:

	def removeAllDuplicates(self, head):
		if head.next == None:
			return head
		ans = Node(0)
		a = ans
		first = head
		second = head.next
		boo = False
		while second:
			if first.data == second.data:
				boo = True
				second = second.next
			elif boo == True:
				while first.next != second:
					first = first.next
				first.next = None
				boo = False
				first = second
				second = second.next
			else:
				first.next = None
				a.next = first
				a = a.next
				first = second
				second = second.next
		if first:
			if not boo:
				a.next = first
		return ans.next


class Solution:

	def removeAllDuplicates(self, head):
		dummy_head = Node(0)
		dummy_head.next = head
		prev = dummy_head
		cur = head
		while cur and cur.next:
			if cur.next.data != cur.data:
				prev = cur
				cur = cur.next
			else:
				while cur.next and cur.data == cur.next.data:
					cur = cur.next
				prev.next = cur.next
				cur = cur.next
		return dummy_head.next


from collections import defaultdict

class Solution:

	def removeAllDuplicates(self, head):
		nodemap = defaultdict(int)
		root = head
		while root is not None:
			nodemap[root.data] += 1
			root = root.next
		root = head
		while root is not None:
			if nodemap[root.data] > 1:
				if root.next is None:
					root.data = None
				else:
					root.data = root.next.data
					root.next = root.next.next
			else:
				root = root.next
		root = head
		while root.next is not None and root.next.data is not None:
			root = root.next
		root.next = None
		if head.data is None:
			return None
		return head


class Solution:

	def removeAllDuplicates(self, head):
		p = r = head
		while p.next != None:
			if p.next.data == p.data:
				q = p
				while q and q.data == p.data:
					q = q.next
				if p == head:
					head = q
				else:
					r.next = q
				p = r
			else:
				r = p
				p = p.next
		return head


def insert(head, data):
	new_node = Node(data)
	if head == None:
		head = new_node
		return head
	cur = head
	while cur.next:
		cur = cur.next
	cur.next = new_node
	return head

class Solution:

	def removeAllDuplicates(self, head):
		d = {}
		cur = head
		while cur:
			if cur.data not in d:
				d[cur.data] = 1
			else:
				d[cur.data] += 1
			cur = cur.next
		j = set()
		for (keys, value) in d.items():
			if value <= 1:
				j.add(keys)
		kl = None
		temp = head
		while temp:
			if temp.data in j:
				kl = insert(kl, temp.data)
			temp = temp.next
		return kl


class Solution:

	def removeAllDuplicates(self, head):
		hm = {}
		curr = head
		while curr:
			if curr.next != None:
				if curr.data == curr.next.data:
					hm[curr.data] = 1
			curr = curr.next
		prev = None
		curr = head
		nxt = curr.next
		while curr:
			if hm.get(curr.data):
				if curr == head:
					head = head.next
					curr = nxt
					if nxt:
						nxt = nxt.next
				else:
					prev.next = nxt
					curr = nxt
					if nxt:
						nxt = nxt.next
			else:
				prev = curr
				curr = nxt
				if nxt:
					nxt = nxt.next
		return head


class Solution:

	def removeAllDuplicates(self, head):
		if head is None or head.next is None:
			return head
		p = None
		l = head
		flag = 0
		while l and l.next:
			if l.data == l.next.data:
				flag = 1
				l = l.next
			elif flag == 1:
				if p is not None:
					p.next = l.next
					l.next = None
					l = p.next
					flag = 0
				else:
					head = l.next
					l.next = None
					l = head
					flag = 0
			else:
				p = l
				l = l.next
		if p is not None:
			if p.next != l:
				p.next = None
		return head


class Solution:

	def removeAllDuplicates(self, head):
		cur = head
		dummy = Node(-1)
		dummy.next = head
		prev = dummy
		while cur and cur.next:
			if cur.data == cur.next.data:
				while cur.next and cur.data == cur.next.data:
					cur = cur.next
				prev.next = cur.next
				cur = cur.next
			else:
				prev = cur
				cur = cur.next
		return dummy.next


class Solution:

	def removeAllDuplicates(self, head):
		cur = head
		head = prev = Node(None)
		head.next = cur
		while cur and cur.next:
			if cur.data == cur.next.data:
				while cur and cur.next and (cur.data == cur.next.data):
					cur = cur.next
				cur = cur.next
				prev.next = cur
			else:
				prev = prev.next
				cur = cur.next
		return head.next


class Solution:

	def removeAllDuplicates(self, head):
		(t, l, d) = (head, [], {})
		while t:
			if t.data in d:
				if t.data in l:
					l.remove(t.data)
			else:
				d[t.data] = 1
				l.append(t.data)
			t = t.next
		h = LinkedList()
		for i in l:
			h.append(i)
		return h.head


class Solution:

	def removeAllDuplicates(self, head):
		dic = {}
		curr = head
		while curr:
			if curr.data in dic:
				dic[curr.data] += 1
			else:
				dic[curr.data] = 1
			curr = curr.next
		ll = LinkedList()
		for i in sorted(dic):
			if dic[i] == 1:
				ll.append(i)
		return ll.head


class Solution:

	def removeAllDuplicates(self, head):
		ret = res = Node(0)
		curr = head
		while curr and curr.next:
			if curr.data == curr.next.data:
				while curr and curr.next and (curr.data == curr.next.data):
					curr = curr.next
				curr = curr.next
			else:
				ret.next = curr
				curr = curr.next if curr else curr
				ret = ret.next if ret else ret
				ret.next = None
		if curr:
			ret.next = curr
			ret = ret.next
		return res.next


class Solution:

	def removeAllDuplicates(self, head):
		d = {}
		n = head
		while True:
			v = n.data
			d[v] = d.get(v, 0) + 1
			if not n.next:
				break
			n = n.next
		n = dmy = Node('')
		dmy.next = head
		while True:
			if not n.next:
				break
			v = n.next.data
			if d[v] > 1:
				if n.next.next:
					n.next = n.next.next
				else:
					n.next = None
					break
			else:
				n = n.next
		return dmy.next


from collections import defaultdict

class Solution:

	def removeAllDuplicates(self, head):
		curr = head
		no_duplicates = defaultdict(lambda : 0)
		while curr:
			no_duplicates[curr.data] += 1
			curr = curr.next
		curr = head
		dummy = head
		prev = None
		while curr:
			if no_duplicates[curr.data] == 1:
				prev = dummy
				dummy.data = curr.data
				dummy = dummy.next
			curr = curr.next
		prev.next = None
		return head


class Solution:

	def removeAllDuplicates(self, head):
		prev = Node(-1)
		x = prev
		temp = head
		a = {}
		while temp != None:
			if temp.next != None and temp.next.data == temp.data:
				a[temp.data] = 1
			elif temp.data not in a:
				prev.next = Node(temp.data)
				prev = prev.next
			temp = temp.next
		head = x.next
		return head


class Solution:

	def removeAllDuplicates(self, head):
		d = dict()
		itr = head
		while itr:
			if itr.data not in d.keys():
				d[itr.data] = 1
			else:
				d[itr.data] += 1
			itr = itr.next
		itr = head
		while itr.next:
			if d[itr.next.data] > 1:
				itr.next = itr.next.next
			else:
				itr = itr.next
		if d[head.data] > 1:
			head = head.next
		return head


class Solution:

	def removeAllDuplicates(self, head):
		dummy = Node(-1)
		itr = dummy
		itr.next = head
		cur = head.next
		while cur:
			f = 0
			while cur and itr.next.data == cur.data:
				f = 1
				cur = cur.next
			if f == 1:
				itr.next = cur
			else:
				itr = itr.next
			if cur:
				cur = cur.next
		return dummy.next