Given a doubly linked list and a position. The task is to delete a node from given position in a doubly linked list. 
Example 1:
Input:
LinkedList = 1 <--> 3 <--> 4 
x = 3
Output: 1 3  
Explanation: After deleting the node at
position 3 (position starts from 1),
the linked list will be now as 1->3.
Example 2:
Input:
LinkedList = 1 <--> 5 <--> 2 <--> 9  
x = 1
Output: 5 2 9
Your Task:
The task is to complete the function deleteNode() which should delete the node at given position and return the head of the linkedlist.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
Constraints:
2 <= size of the linked list <= 1000
1 <= x <= N

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

'''class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None
'''
class Solution:
    def deleteNode(self,head, x):
        # Code here


Solutions:
class Solution:

	def deleteNode(self, head, x):
		if x == 1:
			head = head.next
			if head:
				head.prev = None
			return head
		curr = head.next
		prevs = head
		while x - 2 > 0:
			curr = curr.next
			prevs = prevs.next
			x -= 1
		prevs.next = prevs.next.next
		if prevs.next:
			prevs.next.prev = prevs
		return head


class Solution(object):

	def deleteNode(self, head, p):
		(curr, position) = (head, 1)
		while curr:
			if position == p:
				if curr.prev:
					curr.prev.next = curr.next
				if curr.next:
					curr.next.prev = curr.prev
			position += 1
			curr = curr.next
		return head


class Solution:

	def deleteNode(self, head, x):
		n = head
		for i in range(1, x):
			n = n.next
		if x == 1:
			head = head.next
			head.prev = None
		elif n.next:
			n.prev.next = n.next
			n.next.prev = n.prev
		else:
			n.prev.next = None
		return head


class Solution:

	def deleteNode(self, head, x):
		pos = 1
		curr = head
		while curr != None:
			if pos == x:
				if curr.prev != None:
					curr.prev.next = curr.next
				if curr.next != None:
					curr.next.prev = curr.prev
			curr = curr.next
			pos += 1
		return head


class Solution:

	def deleteNode(self, head, x):
		if x == 1:
			head = head.next
			head.prev = None
		else:
			curr = head
			while x - 1:
				curr = curr.next
				x -= 1
			curr.prev.next = curr.next
			if curr.next:
				curr.next.prev = curr.prev


class Solution:

	def deleteNode(self, head, x):
		if x == 1:
			c = head.next
			c.prev = None
			return c
		p = head
		for _ in range(x - 2):
			p = p.next
		c = p.next
		p.next = c.next
		if c.next:
			c.next.prev = p
		return head


class Solution:

	def deleteNode(self, head, x):
		dummy = Node(0)
		dummy.next = head
		prev = dummy
		curr = dummy.next
		for i in range(x - 1):
			prev = curr
			curr = curr.next
		if curr.next is not None:
			curr.next.prev = prev
		prev.next = curr.next
		del curr
		dummy.next.prev = None
		return dummy.next


class Solution:

	def deleteNode(self, head, x):
		if head is None:
			return None
		if x == 1:
			head = head.next
			head.prev = None
			return head
		pos = 1
		curr = head
		while curr is not None:
			if pos == x:
				if curr.next == None:
					temp = curr.prev
					temp.next = None
					return head
				else:
					temp = curr.prev
					nxt = curr.next
					temp.next = nxt
					nxt.prev = temp
					return head
			curr = curr.next
			pos += 1


class Solution:

	def deleteNode(self, head, x):
		count = 1
		cur = head
		while cur and count != x:
			cur = cur.next
			count += 1
		if cur is None:
			return head
		if cur.prev is None:
			head = cur.next
		else:
			cur.prev.next = cur.next
		if cur.next is not None:
			cur.next.prev = cur.prev
		return head


class Solution:

	def deleteNode(self, head, x):
		if x == 1:
			head = head.next
			head.prev = None
			return head
		p = 1
		t = head
		while t != None:
			if p == x - 1:
				n = t.next
				t.next = n.next
				if n.next != None:
					n.next.prev = t
				break
			p += 1
			t = t.next
		return head


class Solution:

	def deleteNode(self, head, x):
		counter = 1
		temp = head
		while counter < x:
			counter += 1
			temp = temp.next
		if temp.prev == None:
			temp.next.prev = temp.prev
		elif temp.next == None:
			temp.prev.next = temp.next
		else:
			temp.prev.next = temp.next
			temp.next.prev = temp.prev
		return head


class Solution:

	def deleteNode(self, head, x):
		node = llist.head
		x -= 1
		while x != 0:
			node = node.next
			x -= 1
		if head is None or node is None:
			return
		if head == node:
			head = node.next
		if node.next is not None:
			node.next.prev = node.prev
		if node.prev is not None:
			node.prev.next = node.next
		del node


class Solution:

	def deleteNode(self, head, x):
		count = 0
		while head:
			count += 1
			if count == x:
				break
			head = head.next
		if head is None:
			return
		if head.next is None and head.prev is None:
			head = None
			return head
		if head.next is None and head.prev is not None:
			head.prev.next = None
			head.prev = None
			return head
		if head.prev is None and head.next is not None:
			next_node = head.next
			head.next.prev = None
			head = next_node
			return head
		prev_node = head.prev
		prev_node.next = head.next
		head.next.prev = prev_node
		return head


class Solution:

	def deleteNode(self, head, x):
		cur_node = head
		for i in range(x - 1):
			cur_node = cur_node.next
		if cur_node == head:
			head = cur_node.next
			head.prev = None
		elif cur_node.next is None:
			cur_node.prev.next = None
		else:
			cur_node.prev.next = cur_node.next


class Solution:

	def deleteNode(self, head, x):
		if x == 1:
			head.next.prev = None
			return head.next
		else:
			temp = head
			while x != 1:
				head = head.next
				x -= 1
			if head.next is None:
				head.prev.next = None
				return temp
			prevs = head.prev
			prevs.next = head.next
			head.next.prev = prevs
			return temp


class Solution:

	def deleteNode(self, head, x):
		if head is None:
			return
		if x == 1:
			node = head.next
			if node:
				node.prev = None
				del head
			return node
		count = 1
		curr = head
		while curr is not None and count != x:
			count += 1
			curr = curr.next
		prevnode = curr.prev
		nextnode = curr.next
		prevnode.next = nextnode
		if nextnode:
			nextnode.prev = prevnode
		del curr
		return head


class Solution:

	def deleteNode(self, head, x):
		i = 1
		n = head
		if x == 1:
			n = n.next
			n.prev = None
		else:
			while i < x - 1:
				n = n.next
				i += 1
			if n.next.next == None:
				n.next.prev = None
				n.next = None
			else:
				n.next = n.next.next
				n.next.prev = n
		return head


class Solution:

	def deleteNode(self, head, x):
		if head is None:
			return
		curr = head
		cnt = 0
		if x == 1:
			curr = curr.next
			curr.prev = None
			return curr
		while curr:
			cnt += 1
			if cnt == x:
				curr.prev.next = curr.next
			curr = curr.next
		return head


class Solution:

	def deleteNode(self, head, x):
		temp = head
		for i in range(1, x):
			prev2 = temp
			temp = temp.next
		if not temp.next:
			temp.prev.next = temp.next
		elif temp == head:
			head = temp.next
			head.prev = None
		else:
			temp.prev.next = temp.next


class Solution:

	def deleteNode(self, head, x):
		temp = head
		count = 2
		if x == 1:
			temp.next.prev = None
			head = temp.next
		else:
			while count < x:
				temp = temp.next
				count += 1
			temp.next = temp.next.next


class Solution:

	def deleteNode(self, head, x):
		temp = head
		for i in range(x - 1):
			temp = temp.next
		if temp.next is None:
			temp.prev.next = None
			temp.prev = None
		elif temp.prev and temp.next is not None:
			temp.prev.next = temp.next
			temp.next.prev = temp.prev
			temp.prev = None
			temp.next = None
		elif temp.prev is None:
			temp = temp.next
			temp.prev = None


class Solution:

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


class Solution:

	def deleteNode(self, head, x):
		curr = head
		count = 0
		while curr != None:
			if x == 1:
				head = head.next
				head.prev = None
				return head
			count += 1
			if count == x:
				curr.prev.next = curr.next
				if curr.next != None:
					curr.next.prev = curr.prev
				return head
			curr = curr.next


class Solution:

	def deleteNode(self, head, x):
		curr = head
		if x == 1:
			curr.data = curr.next.data
			if curr.next.next:
				curr.next.next.prev = curr
			curr.next = curr.next.next
		else:
			for _ in range(1, x):
				curr = curr.next
			curr.prev.next = curr.next
			if curr.next:
				curr.next.prev = curr.prev
			curr.next = None
			curr.prev = None


class Solution:

	def deleteNode(self, head, k):
		temp = head
		if k == 1:
			temp.next.prev = None
			return temp.next
		for i in range(k - 2):
			temp = temp.next
		node_to_delete = temp.next
		temp.next = node_to_delete.next
		if node_to_delete.next is None:
			node_to_delete.prev = None
		else:
			node_to_delete.next.prev = temp
		return head


class Solution:

	def deleteNode(self, head, x):
		temp = head
		count = 0
		if x == 1:
			temp = temp.next
			temp.prev = None
			return
		while temp is not None:
			count += 1
			if count == x:
				break
			temp = temp.next
		if temp.next is None:
			temp.prev.next = None
			return
		if temp.next is not None:
			temp.prev.next = temp.next
			temp.next.prev = temp.prev


class Solution:

	def deleteNode(self, head, x):
		if head is None:
			return None
		if x == 1:
			head = head.next
			if head is not None:
				head.prev = None
			return head
		current = head
		position = 1
		while current is not None and position < x:
			current = current.next
			position += 1
		if current is None:
			return head
		prev_node = current.prev
		next_node = current.next
		if prev_node is not None:
			prev_node.next = next_node
		if next_node is not None:
			next_node.prev = prev_node
		return head


class Solution:

	def deleteNode(self, head, x):
		c = 1
		temp = head
		if x == 1:
			head = head.next
			head.prev = None
			return head.next
		while c < x - 1:
			temp = temp.next
			c += 1
		if temp.next.next:
			temp.next = temp.next.next
			temp.next.prev = temp
		else:
			temp.next = None
		return head


class Solution:

	def deleteNode(self, head, x):
		start = Node(0)
		start.next = head
		p1 = start
		while x:
			x -= 1
			p1 = p1.next
		if p1.prev:
			p1.prev.next = p1.next
		if p1.next:
			p1.next.prev = p1.prev
		return start.next


class Solution:

	def deleteNode(self, head, x):
		cnt = 0
		cur = head
		prev = None
		if x == 1:
			head.next.prev = None
			return head.next
		while cur != None:
			cnt += 1
			if cnt == x:
				prev.next = cur.next
				if cur.next != None:
					n = cur.next
					n.prev = prev
			prev = cur
			cur = cur.next
		return head


class Solution:

	def deleteNode(self, head, x):
		if x == 1:
			tmp = head.next
			tmp.prev = None
			return tmp
		curr = head
		for i in range(x - 1):
			curr = curr.next
		prev_node = curr.prev
		prev_node.next = curr.next
		return head


class Solution:

	def deleteNode(self, head, location):
		index = 0
		temp = head
		while index < location - 1:
			temp = temp.next
			index += 1
		if temp == head:
			temp.next.prev = None
			head = temp.next
		elif temp.next == None:
			temp.prev.next = None
		else:
			temp.prev.next = temp.next
			temp.next.prev = temp.prev
		return head


class Solution:

	def deleteNode(self, head, x):
		curr = head
		Prev = None
		n = 0
		while curr != None:
			n += 1
			curr = curr.next
		curr = head
		k = 1
		while k < x:
			Prev = curr
			curr = curr.next
			k += 1
		if x == n:
			Prev.next = None
		elif x == 1:
			head = head.next
			head.prev = None
		else:
			Prev.next = curr.next
			curr.next.prev = curr.prev
		return head


class Solution:

	def deleteNode(self, head, x):
		ptr = head
		d = Node(-1)
		d.next = head
		head.prev = d
		while x > 1:
			ptr = ptr.next
			x -= 1
		ptr.prev.next = ptr.next
		if ptr.next:
			ptr.next.prev = ptr.prev
		head = d.next
		head.prev = None
		return head


class Solution:

	def deleteNode(self, head, x):
		cur = head
		for _ in range(x - 1):
			cur = cur.next
		if x == 1:
			head = head.next
			head.prev = None
		elif cur.next:
			cur.prev.next = cur.next
			cur.next.prev = cur.prev
		else:
			cur.prev.next = None
			cur.prev = None
		return head


class Solution:

	def deleteNode(self, head, x):
		if x == 1:
			head = head.next
			head.prev = None
			return head
		start_pos = 1
		start = head
		prev = head
		while start_pos < x:
			start = start.next
			prev = prev.next
			start_pos += 1
		if start.next is None:
			start.prev.next = None
		else:
			start = start.next
			prev = prev.prev
			start.prev = prev
			prev.next = start
		return head


class Solution:

	def deleteNode(self, head, pos):
		if head is None:
			return None
		if pos == 1:
			head.data = head.next.data
			head.next.prev = None
			head.next = head.next.next
			return head
		c = 1
		curr = head
		while c != pos and curr is not None:
			c += 1
			curr = curr.next
		if curr is None:
			return head
		if curr.next is None:
			curr.prev.next = None
			curr.prev = None
			return head
		temp = curr.next
		temp.prev = curr.prev
		curr.prev.next = temp
		return head


class Solution:

	def deleteNode(self, head, x):
		temp = None
		curr = head
		for i in range(x - 1):
			temp = curr
			curr = curr.next
		if curr.next is None:
			temp.next = None
		elif curr.prev is None:
			curr = curr.next
			curr.prev = None
			head = curr
			return head
		else:
			nex = curr.next
			temp.next = nex
			nex.prev = temp
		return head


class Node:

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

class Solution:

	def deleteNode(self, head, x):
		curNode = head
		for i in range(1, x):
			curNode = curNode.next
		if x == 1:
			head = head.next
			head.prev = None
		elif curNode.next:
			curNode.prev.next = curNode.next
			curNode.next.prev = curNode.prev
		else:
			curNode.prev.next = None
		return head


class Solution:

	def deleteNode(self, head, x):
		t = head
		c = 1
		if x == 1:
			head = head.next
			head.prev = None
			return head
		t = t.next
		while t.next != None:
			c = c + 1
			if c == x:
				a = t.prev
				a.next = a.next.next
				a.next.prev = a
				return head
			t = t.next
		a = t.prev
		a.next = None
		return head


class Solution:

	def deleteNode(self, head, x):
		count = 1
		current = head
		if x == 1:
			head = head.next
			head.prev = None
			return
		while count < x:
			count += 1
			current = current.next
		current.prev.next = current.next
		if current.next:
			current.next.prev = current.prev


class Solution:

	def deleteNode(self, head, x):
		if x == 1:
			head = head.next
			head.prev = None
			return head
		cur = head
		for i in range(x - 1):
			if cur.next:
				cur = cur.next
			else:
				return head
		if cur.next is not None:
			cur.prev.next = cur.next
		else:
			cur.prev.next = None
		return head


class Solution:

	def deleteNode(self, head, x):
		iter = 1
		curr = head
		while iter < x:
			curr = curr.next
			iter += 1
		if curr.prev is None:
			head = head.next
			head.prev = None
			return head
		elif curr.next is None:
			curr.prev.next = None
			return head
		else:
			curr.prev.next = curr.next
			curr.next.prev = curr.prev
			return head


class Solution:

	def deleteNode(self, head, x):
		if x is 1:
			head = head.next
			head.prev = None
			return
		cur = 1
		temp = head
		while True:
			if cur == x:
				break
			prev = temp
			temp = temp.next
			cur += 1
		if temp.next:
			prev.next = temp.next
			temp.next.prev = prev
		else:
			prev.next = None
		return head


class Solution:

	def deleteNode(self, head, x):
		if x == 1:
			head = head.next
			head.prev = None
			return head
		a = head
		for i in range(1, x - 1):
			a = a.next
		if a.next != None:
			a.next = a.next.next
		return head


class Solution:

	def deleteNode(self, head, x):
		if x <= 0:
			return head
		if x == 1:
			if head is None:
				return None
			elif head.next is None:
				return None
			else:
				head = head.next
				head.prev = None
				return head
		node = head
		pos = 0
		while node:
			pos += 1
			if pos == x and node.next != None:
				node.prev.next = node.next
				node.next.prev = node.prev
			elif pos == x and node.next == None:
				node.prev.next = None
			else:
				node = node.next
		return head


class Solution:

	def deleteNode(self, head, x):
		temp = head
		if x == 1:
			head.next.prev = None
			return head.next
		x -= 1
		while head and x:
			head = head.next
			x -= 1
		if not head:
			return temp
		if head.next == None:
			head.prev.next = None
			return temp
		head.prev.next = head.next
		head.next.prev = head.prev
		return temp


class Solution:

	def deleteNode(self, head, x):
		if head is None and head.next is None:
			return None
		if x == 1:
			curr = head.next
			curr.prev = None
			return curr
		curr = head
		for ele in range(x - 2):
			curr = curr.next
		curr.next = curr.next.next
		if curr.next is None:
			return head
		curr.next.prev = curr
		return head


class Solution:

	def deleteNode(self, head, x):
		temp = head
		for i in range(1, x):
			temp = temp.next
		if temp.prev is None:
			head = temp.next
		else:
			temp.prev.next = temp.next
		if temp.next is None:
			tail = temp.prev
		else:
			temp.next.prev = temp.prev


class Solution:

	def deleteNode(self, head, x):
		temp = head
		c = 0
		while temp != None:
			c += 1
			if c == x:
				if temp.prev == None:
					temp.next.prev = None
					root = temp.next
					break
				if temp.next == None:
					temp.prev.next = None
					break
				else:
					temp.prev.next = temp.next
					temp.next.prev = temp.prev
					break
			temp = temp.next