Given a singly linked list and an integer x.Delete x^{th} node from the singly linked list. 
Example 1: 
Input: 1 -> 3 -> 4 
       x = 3
Output: 1 -> 3
Explanation:
After deleting the node at 3rd
position (1-base indexing), the
linked list is as 1 -> 3. 
Example 2:
Input: 1 -> 5 -> 2 -> 9 
x = 2
Output: 1 -> 2 -> 9
Explanation: 
After deleting the node at 2nd
position (1-based indexing), the
linked list is as 1 -> 2 -> 9.
 
Your task: Your task is to complete the method deleteNode() which takes two arguments: the address of the head of the linked list and an integer x. The function returns the head of the modified linked list.
Constraints:
1 <= T <= 300
2 <= N <= 100
1 <= x <= N

Difficulty: EASY
Tags: Data structures
Skill types: Data structures

Starter code:
# your task is to complete this function

# function should return new head pointer



'''

class node:

    def __init__(self):

        self.data = None

        self.next = None

'''



def delNode(head, k):

    # Code here

Solutions:
def delNode(head, k):
	if k == 1:
		return head.next
	curr = head
	while k > 2:
		(curr, k) = (curr.next, k - 1)
	curr.next = curr.next.next
	return head


def delNode(head, x):
	temp = head
	count = 0
	if k == 1:
		head = head.next
	else:
		while temp and temp.next:
			count += 1
			if count == k - 1:
				temp.next = temp.next.next
				break
			temp = temp.next
	return head


def delNode(head, k):
	curr = head
	if k == 1 and curr:
		head = curr.next
		curr = None
		return head
	pos = 1
	while curr:
		if pos == k:
			break
		prev = curr
		curr = curr.next
		pos += 1
	if curr is None:
		return head
	prev.next = curr.next
	curr = None
	return head


def delNode(head, k):
	if head == None:
		return None
	temp = head
	if k == 1:
		return head.next
	for i in range(k - 2):
		temp = temp.next
	if temp.next is not None:
		temp.next = temp.next.next
	return head


class node:

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

def delNode(head, k):
	temp = head
	prev = head
	for i in range(0, k):
		if i == 0 and k == 1:
			head = head.next
		elif i == k - 1 and temp is not None:
			prev.next = temp.next
		else:
			prev = temp
			if prev is None:
				break
			temp = temp.next
	return head


def delNode(head, k):
	temp = head
	prev = None
	c = 0
	if k == 1:
		return head.next
	while temp != None:
		c += 1
		if c == k:
			prev.next = temp.next
			temp.next = None
		else:
			prev = temp
			temp = temp.next
	return head


def delNode(head, k):
	if k == 1:
		temp = head
		head = head.next
		return head
	i = 1
	temp = head
	while temp.next is not None:
		if i + 1 == k:
			temp.next = temp.next.next
			return head
		temp = temp.next
		i += 1


def delNode(head, k):
	curr = head
	if k == 1:
		return head.next
	for i in range(k - 2):
		curr = curr.next
	curr.next = curr.next.next
	return head


def delNode(head, k):
	if not head:
		return None
	if k == 1:
		head = head.next
		return head
	curr = head
	prev = None
	count = 1
	while curr and count < k:
		prev = curr
		curr = curr.next
		count += 1
	if count < k or curr is None:
		return head
	prev.next = curr.next
	return head


def delNode(head, k):
	temp = 1
	itr = head
	prev = head
	if k == 1:
		head = head.next
		return head
	while temp != k:
		prev = itr
		itr = itr.next
		temp += 1
	prev.next = itr.next
	return head


def delNode(head, k):
	if k == 1:
		return head.next
	p = head
	for _ in range(k - 2):
		p = p.next
	p.next = p.next.next
	return head


def delNode(head, x):
	temp = head
	if x == 1:
		return head.next
	c = 2
	prev = head
	while temp:
		if c == x:
			temp.next = temp.next.next
		temp = temp.next
		c += 1
	return head


def delNode(head, k):
	if head is None:
		return None
	elif head.next is None:
		return None
	elif k is 1:
		return head.next
	else:
		current = head
		count = 0
		while count < k - 2:
			count += 1
			current = current.next
		current.next = current.next.next
		return head


def delNode(head, k):
	if k == 1:
		return head.next
	temp = head
	cnt = 1
	temp2 = head
	while k != cnt and temp is not None:
		temp2 = temp
		temp = temp.next
		cnt += 1
	temp2.next = temp.next
	del temp
	return head


def delNode(head, k):
	if not head:
		return None
	if k == 1:
		return head.next
	else:
		ptr = head
		for i in range(k - 2):
			ptr = ptr.next
		if not ptr.next.next:
			ptr.next = None
		else:
			ptr.next = ptr.next.next
		return head


def delNode(head, k):
	p = q = head
	c = 1
	if k == 1:
		head = head.next
	else:
		while c != k:
			c += 1
			q = p
			p = p.next
		p = p.next
		q.next = p
	return head


def delNode(head, k):
	curr = head
	prev = None
	if k == 1:
		curr = curr.next
		return curr
	for i in range(k - 1):
		prev = curr
		curr = curr.next
	prev.next = curr.next
	curr.next = None
	return head


def delNode(head, k):
	dummy = node()
	dummy.next = head
	prev = dummy
	curr = head
	for i in range(k - 1):
		prev = curr
		curr = curr.next
	prev.next = prev.next.next
	del curr
	return dummy.next


def delNode(head, k):
	temp = head
	if k == 1:
		head = temp.next
		return head
	for _ in range(k - 2):
		temp = temp.next
	if temp.next.next == None:
		temp.next = None
	else:
		temp.next = temp.next.next
	return head


def delNode(head, k):
	if head is None:
		return
	if k == 1:
		head = head.next
		return head
	l = 1
	cur = head
	while l != k - 1:
		cur = cur.next
		l += 1
	cur.next = cur.next.next
	return head


def delNode(head, k):
	if not head:
		return head
	if k == 1:
		return head.next
	pre = None
	cur = head
	index = 1
	while index <= k and cur:
		nex = cur.next
		if index == k:
			pre.next = nex
			cur.next = None
			cur = nex
			break
		else:
			pre = cur
			cur = nex
		index += 1
	return head


def delNode(head, k):
	if head is None:
		return head
	if k == 1:
		return head.next
	cur = head
	prev = None
	count = 1
	while cur and count < k:
		prev = cur
		cur = cur.next
		count += 1
	prev.next = cur.next
	return head


def delNode(head, k):
	curr = head
	count = 1
	while curr.next != None:
		curr = curr.next
		count += 1
	if k > count:
		return
	elif k == 1:
		return head.next
	else:
		curr = head
		for i in range(k - 2):
			curr = curr.next
		curr.next = curr.next.next
		return head


def delNode(head, k):
	prev = None
	curr = head
	k = k - 1
	while k and curr:
		(prev, curr) = (curr, curr.next)
		k = k - 1
	if not prev:
		head = head.next
		return head
	elif not curr:
		return head
	else:
		prev.next = curr.next
		return head


def delNode(head, k):
	c = 1
	x = head
	if k == 1:
		head = head.next
	while x:
		if c == k - 1 and x.next.next == None:
			x.next = None
		elif c == k - 1 and x.next.next != None:
			x.next = x.next.next
		x = x.next
		c += 1
	return head


def delNode(head, k):
	curr = head
	if k == 1:
		return head.next
	if curr is None:
		return curr
	i = 2
	while curr:
		if i == k:
			curr.next = curr.next.next
		curr = curr.next
		i += 1
	return head


def delNode(head, k):
	if head is None:
		return
	if k == 1:
		return head.next
	n = head
	x = 1
	while x != k:
		x += 1
		prev = n
		n = n.next
	prev.next = prev.next.next
	return head


def delNode(head, k):
	temp = head
	count = 1
	while temp.next != None:
		if k == 1:
			head = head.next
			break
		prev = temp
		temp = temp.next
		count += 1
		if count == k:
			prev.next = temp.next
			break
	temp = head
	l = []
	while temp != None:
		l.append(temp.data)
		temp = temp.next
	return head


def delNode(head, k):
	next_node = head
	prev_node = None
	counter = 1
	while next_node:
		if counter == k:
			if next_node.next:
				if prev_node:
					prev_node.next = next_node.next
					return head
				else:
					head = next_node.next
					return head
			elif prev_node:
				prev_node.next = None
				return head
			else:
				head = None
				return head
		prev_node = next_node
		next_node = next_node.next
		counter += 1


def delNode(head, k):
	if head is None:
		return None
	if k == 1:
		head = head.next
		return head
	temp = head
	prev = head
	for i in range(0, k - 1):
		if prev is None:
			return head
		prev = temp
		temp = temp.next
	prev.next = temp.next
	temp = None
	return head


def delNode(head, k):
	if head is None:
		return head
	if k == 1:
		return head.next
	curr = head
	k -= 1
	while curr.next is not None and k != 1:
		curr = curr.next
		k -= 1
	if curr.next is not None:
		node = curr.next
		curr.next = curr.next.next
		del node
	return head


def delNode(head, k):
	if head is None:
		return head
	a = head
	temp = head.next
	if k == 1:
		head = head.next
		return head
	for i in range(0, k - 2):
		a = a.next
		temp = temp.next
	a.next = temp.next
	return head


def delNode(head, k):
	cou = 2
	if k == 1:
		return head.next
	cur = head
	while cou < k:
		cur = cur.next
		cou += 1
	cur.next = cur.next.next
	return head


class node:

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

def delNode(head, k):
	if k == 1:
		head = head.next
		return head
	else:
		tempNode = head
		index = 1
		while index < k - 1:
			tempNode = tempNode.next
			index += 1
		nextNode = tempNode.next
		tempNode.next = nextNode.next
		return head


def delNode(head, k):
	if k == 1:
		head = head.next
		return head
	prev = None
	temp = head
	while temp and k > 0:
		k -= 1
		if k == 0:
			prev.next = temp.next
			return head
		prev = temp
		temp = temp.next


def delNode(head, k):
	if k == 0:
		return head
	if k == 1:
		return head.next
	prev = None
	curr = head
	c = 1
	while curr != None:
		if c == k:
			curr = curr.next
			prev.next = curr
			break
		prev = curr
		curr = curr.next
		c += 1
	return head


def delNode(head, k):
	count = 0
	if head is None:
		return
	if head.next == None and head.data == k:
		return None
	if k == 1:
		return head.next
	else:
		curr = head
		while count < k - 2:
			count += 1
			curr = curr.next
		curr.next = curr.next.next
		return head


def delNode(head, k):
	if k == 1:
		temp = head.next
		head.next = None
		head = temp
		return head
	i = 2
	first = head
	second = first.next
	while second.next != None:
		if i == k:
			first.next = second.next
			second.next = None
			return head
		i += 1
		first = first.next
		second = second.next
	if i == k:
		first.next = None
	return head


def delNode(head, k):
	if head == None:
		return None
	if k == 1:
		return head.next
	cur = head
	for i in range(k - 2):
		if cur != None:
			cur = cur.next
	if cur != None:
		cur.next = cur.next.next
		return head


def delNode(head, k):
	current = head
	for i in range(k):
		if k == 1:
			head = current.next
			return head
		elif i == k - 2:
			if current.next.next != None:
				current.next = current.next.next
				return head
			elif current.next.next == None:
				current.next = None
				return head
		current = current.next


def delNode(head, k):
	temp = head
	if k == 1:
		return temp.next
	for i in range(k - 2):
		temp = temp.next
	node_to_delete = temp.next
	temp.next = node_to_delete.next
	return head


def delNode(head, k):
	dummy = node()
	dummy.next = head
	temp = dummy
	ans = 0
	temp1 = temp
	while ans != k:
		temp1 = temp
		temp = temp.next
		ans += 1
	temp1.next = temp1.next.next
	return dummy.next


def delNode(head, k):
	if k == 1:
		head = head.next
	else:
		temp = head
		count = 2
		while temp != None and count < k:
			temp = temp.next
			count += 1
		temp.next = temp.next.next
	return head


def delNode(head, k):
	if k == 1:
		head = head.next
	else:
		temp = head
		for i in range(1, k - 1):
			temp = temp.next
		temp.next = temp.next.next
	return head


def delNode(head, k):
	n = head
	prev = None
	count = 1
	if k == 1:
		head = n.next
		n.next = None
	else:
		while n.next != None:
			count += 1
			prev = n
			n = n.next
			if count == k:
				if n.next == None:
					prev.next = None
				else:
					prev.next = n.next
	return head


def delNode(head, position):
	temp = head
	prev = head
	for i in range(0, position):
		if i == 0 and position == 1:
			head = head.next
		elif i == position - 1 and temp is not None:
			prev.next = temp.next
		else:
			prev = temp
			if prev is None:
				break
			temp = temp.next
	return head


def delNode(head, k):
	temp = head
	if k == 1:
		return head.next
	c = 1
	while c < k - 1:
		temp = temp.next
		c += 1
	temp.next = temp.next.next
	return head


def delNode(head, k):
	if k == 1:
		return head.next
	current = head
	while k > 2:
		current = current.next
		k -= 1
	current.next = current.next.next
	return head


class Node:

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

def delNode(head, k):
	if not head:
		return head
	if k == 1:
		return head.next
	prev_node = None
	curr_node = head
	for i in range(k - 1):
		if not curr_node:
			return head
		prev_node = curr_node
		curr_node = curr_node.next
	if prev_node:
		prev_node.next = curr_node.next
	else:
		head = curr_node.next
	curr_node = None
	return head


def delNode(head, k):
	if head == None:
		return temp
	if k == 1:
		return head.next
	curr = head
	count = 0
	while curr != None:
		count += 1
		if count == k - 1:
			curr.next = curr.next.next
			break
		curr = curr.next
	return head


def delNode(head, k):
	count = 1
	prev = None
	curr = head
	while count < k:
		prev = curr
		curr = curr.next
		count += 1
	if prev is not None:
		prev.next = curr.next
	else:
		head = curr.next
	return head


def delNode(head, k):
	if k == 1:
		head = head.next
	else:
		index = 1
		temp = head
		while index < k - 1:
			temp = temp.next
			index += 1
		temp.next = temp.next.next
	return head


def delNode(head, k):
	a = []
	while head != None:
		a.append(head.data)
		head = head.next
	a.pop(k - 1)
	for i in a:
		print(i, end=' ')


def delNode(head, k):
	index = 1
	tempNode = head
	if k == 1:
		head = tempNode.next
		return head
	while True:
		if index == k:
			prevNode.next = tempNode.next
			tempNode.next = None
			return head
		prevNode = tempNode
		tempNode = tempNode.next
		index += 1
	return head


def delNode(head, k):
	temp = head
	if k == 1:
		head = temp.next
		return head
	cnt = 1
	while temp.next != None and cnt < k - 1:
		temp = temp.next
		cnt += 1
	temp.next = temp.next.next
	return head


def delNode(head, k):
	if k == 1:
		head = head.next
		return head
	p = head
	q = head.next
	x = 1
	while x != k - 1:
		p = p.next
		q = q.next
		x += 1
	p.next = q.next
	return head


def delNode(head, k):
	counter = 1
	current = head
	while head.next and counter != k:
		previous = current
		current = current.next
		counter += 1
	if counter == 1:
		return head.next
	elif current.next:
		previous.next = current.next
		current.next = None
		return head
	else:
		previous.next = None
		return head


def delNode(head, k):
	if head is None:
		return head
	ptr = head
	count = 1
	prev = None
	if k == 1:
		head = head.next
		return head
	else:
		while ptr is not None and count < k:
			prev = ptr
			ptr = ptr.next
			count += 1
		prev.next = ptr.next
		return head


def delNode(head, k):
	if k == 1:
		head = head.next
		return head
	curr = head
	cnt = 1
	while curr != None:
		if cnt == k - 1:
			break
		curr = curr.next
		cnt += 1
	curr.next = curr.next.next
	return head


def delNode(head, k):
	temp = head
	index = 0
	if k == 1:
		return head.next
	while index < k - 2:
		temp = temp.next
		index += 1
	temp.next = temp.next.next
	return head


def delNode(head, k):
	start = head
	count = 1
	if k == 1:
		head = head.next
		return head
	while count < k - 1:
		head = head.next
		count += 1
	if head.next.next == None:
		head.next = None
	else:
		head.next = head.next.next
	head = start
	return head


def delNode(head, k):
	if k == 1:
		return head.next
	curr = head
	count = 1
	while count < k - 1:
		curr = curr.next
		count += 1
	curr.next = curr.next.next
	return head


def delNode(head, k):
	if k == 1:
		return head.next
	temp = head
	count = 1
	temp2 = head
	while k != count and temp != None:
		temp2 = temp
		temp = temp.next
		count = count + 1
	temp2.next = temp.next
	del temp
	return head


def delNode(head, k):
	i = 1
	a = head
	if k == 1:
		return head.next
	while head and head.next:
		i = i + 1
		if i == k:
			head.next = head.next.next
		head = head.next
	return a