Given a doubly linked list of n elements. The task is to reverse the doubly linked list.
Example 1:
Input:
LinkedList: 3 <--> 4 <--> 5
Output: 5 4 3
Example 2:
Input:
LinkedList: 75 <--> 122 <--> 59 <--> 196
Output: 196 59 122 75
Your Task:
Your task is to complete the given function reverseDLL(), which takes head reference as argument and should reverse the elements so that the tail becomes the new head and all pointers are correctly pointed. You need to return the new head of the reversed list. The printing and verification is done by the driver code.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 <= number of nodes <= 10^{3}
0 <= value of nodes <= 10^{3}

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

'''



def reverseDLL(head):

    #return head after reversing



Solutions:
def reverseDLL(head):
	if head == None:
		return None
	if head.next == None:
		return head
	curr = head
	prev = None
	while curr != None:
		prev = curr
		(curr.next, curr.prev) = (curr.prev, curr.next)
		curr = curr.prev
	return prev


def reverseDLL(head):
	curr = head
	prev = None
	while curr:
		temp = curr.next
		curr.next = prev
		prev = curr
		curr = temp
	return prev


def reverseDLL(head):
	prev = None
	curr = head
	while curr:
		fut = curr.next
		curr.next = prev
		prev = curr
		curr = fut
	return prev


def reverseDLL(head):
	curr = head
	while curr:
		temp = curr.prev
		curr.prev = curr.next
		curr.next = temp
		dummy = curr
		curr = curr.prev
	return dummy


def reverseDLL(head):
	x = []
	ptr = head
	while ptr != None:
		x.append(ptr.data)
		ptr = ptr.next
	ptr = head
	for i in x[::-1]:
		ptr.data = i
		ptr = ptr.next
	return head


def reverseDLL(head):
	newhead = head
	head = head.next
	newhead.next = None
	while head:
		temp = head
		head = head.next
		newhead.prev = temp
		temp.next = newhead
		temp.prev = None
		newhead = temp
	return newhead


def reverseDLL(head):
	(p, c) = (None, head)
	while c:
		c.prev = c.next
		c.next = p
		(p, c) = (c, c.prev)
	return p


def reverseDLL(head):
	temp = head
	after = head.next
	before = None
	while temp:
		after = temp.next
		(temp.prev, temp.next) = (temp.next, temp.prev)
		prev = temp
		temp = after
	return prev


def reverseDLL(head):
	front = head
	while front.next != None:
		temp = front.next
		front.next = front.prev
		front.prev = temp
		front = temp
	temp = front.next
	front.next = front.prev
	front.prev = temp
	return front


def reverseDLL(head):
	itr = head
	prev = None
	while itr:
		nxt = itr.next
		itr.next = itr.prev
		itr.prev = prev
		prev = itr
		itr = nxt
	return prev


def reverseDLL(head):
	p = None
	cur = head
	while cur:
		n = cur.next
		cur.next = p
		cur.prev = n
		p = cur
		cur = n
	return p


def reverseDLL(head):
	if not head:
		return
	while head:
		t = head.next
		head.next = head.prev
		head.prev = t
		if head.prev == None:
			break
		head = head.prev
	return head


def reverseDLL(head):
	curr = head
	prev = None
	while curr is not None:
		temp = curr.next
		curr.next = prev
		if prev is not None:
			prev.prev = curr
		prev = curr
		curr = temp
	if prev is not None:
		prev.prev = None
	return prev


def reverseDLL(head):
	(f, l) = (head, head)
	c = 1
	while l.next:
		l = l.next
		c += 1
	i = 1
	while i <= c // 2:
		temp = f.data
		f.data = l.data
		l.data = temp
		f = f.next
		l = l.prev
		i += 1
	return head


def reverseDLL(head):
	q = head
	while q.next != None:
		q = q.next
	p = head
	while p != q and p.next != q:
		(p.data, q.data) = (q.data, p.data)
		p = p.next
		q = q.prev
	if p != q:
		(p.data, q.data) = (q.data, p.data)
	return head


def reverseDLL(head):
	if head.next == None:
		return head
	t = head
	while t.next != None:
		t = t.next
	temp = t
	t.next = temp.prev
	t.prev = None
	head1 = t
	t = t.next
	while t != None:
		temp = t.prev
		t.prev = t.next
		t.next = temp
		t = t.next
	return head1


def reverseDLL(head):
	newNode = head
	currentNode = head
	prevNode = None
	while newNode != None:
		newNode = newNode.next
		temp = currentNode.next
		currentNode.next = currentNode.prev
		currentNode.prev = temp
		prevNode = currentNode
		currentNode = newNode
	head = prevNode
	return head


def reverseDLL(head):
	curr = head
	p = None
	while curr:
		q = curr.next
		curr.next = p
		curr.prev = q
		p = curr
		curr = q
	return p


def reverseDLL(head):
	p = None
	curr = head
	n = head.next
	while curr.next is not None:
		curr.next = p
		curr.prev = n
		p = curr
		curr = n
		n = n.next
	curr.prev = n
	curr.next = p
	head = curr
	return head


def reverseDLL(head):
	if head == None:
		return None
	if head.next == None:
		return head
	cur = head
	prev = None
	temp = None
	while cur != None:
		temp = cur.prev
		cur.prev = cur.next
		cur.next = temp
		temp = cur
		cur = cur.prev
	return temp


def reverseDLL(head):
	curr = head
	prev = None
	while curr:
		z = curr.next
		curr.prev = z
		curr.next = prev
		prev = curr
		curr = z
	return prev


def reverseDLL(head):
	dummy = None
	while head:
		prev = head.prev
		head.prev = head.next
		head.next = dummy
		dummy = head
		head = head.prev
	return dummy


def reverseDLL(curr):
	head = curr
	prev = None
	if head.next is None:
		return head
	while head:
		p = head.prev
		next = head.next
		head.prev = next
		head.next = p
		head = next
	return p.prev


def reverseDLL(head):
	curr = head
	prev = None
	while curr:
		tmp = curr.prev
		curr.prev = curr.next
		curr.next = tmp
		prev = curr
		curr = curr.prev
	return prev


def reverseDLL(head):
	current = head
	n = None
	p = None
	new_head = None
	while current is not None:
		new_head = current
		n = current.next
		p = current.prev
		current.next = p
		current.prev = n
		current = n
	return new_head


def reverseDLL(head):
	prev = None
	while head:
		ne = head.next
		head.next = prev
		head.prev = ne
		prev = head
		head = ne
	return prev


def reverseDLL(head):
	curr = head
	prev = None
	while curr is not None:
		nextnode = curr.next
		(curr.next, curr.prev) = (curr.prev, curr.next)
		prev = curr
		curr = nextnode
	return prev


def reverseDLL(head):
	cur = head
	prev = None
	nxt = None
	while cur:
		prev = cur.prev
		nxt = cur.next
		cur.prev = nxt
		cur.next = prev
		cur = cur.prev
	if prev is not None:
		head = prev.prev
	return head


def reverseDLL(head):
	temp = head
	t = None
	while temp:
		(temp.next, temp.prev) = (temp.prev, temp.next)
		head = temp
		temp = temp.prev
	return head


def reverseDLL(h):
	s = h
	e = h
	c = 1
	while e.next:
		e = e.next
		c += 1
	c1 = c // 2
	while c1:
		(s.data, e.data) = (e.data, s.data)
		s = s.next
		e = e.prev
		c1 -= 1
	return h


def reverseDLL(head):
	if head is None and head.next is None:
		return head
	current = head
	prev = None
	while current != None:
		next = current.next
		current.next = prev
		current.prev = next
		prev = current
		current = next
	head = prev
	return head


def reverseDLL(head):
	curr = head
	head1 = None
	prev1 = None
	while curr != None:
		if head1 == None:
			head1 = head
			curr = curr.next
			head1.prev = None
			head1.next = None
			prev = head1
		else:
			temp = curr
			curr = curr.next
			temp.next = head1
			head1.prev = temp
			temp.prev = None
			head1 = temp
	return head1


def reverseDLL(head):
	curr = head
	while curr:
		new_node = curr.next
		(curr.next, curr.prev) = (curr.prev, curr.next)
		if not new_node:
			return curr
		curr = new_node
	return curr


def reverseDLL(head):
	if head is None or head.next is None:
		return head
	prevNode = head
	currNode = head.next
	prevNode.next = None
	prevNode.prev = currNode
	while currNode.next is not None:
		temp = currNode.next
		currNode.next = prevNode
		currNode.prev = temp
		prevNode = currNode
		currNode = temp
	currNode.prev = None
	currNode.next = prevNode
	head = currNode
	return head


def reverseDLL(head):
	a = head
	p = []
	while a is not None:
		p.append(a.data)
		a = a.next
	q = p[::-1]
	head1 = None
	a = None
	for i in q:
		m = Node(i)
		if head1 is None:
			head1 = m
			a = head1
		else:
			a.next = m
			m.prev = a
			a = a.next
	return head1


def reverseDLL(head):
	cur = head
	while cur:
		temp = cur.prev
		cur.prev = cur.next
		cur.next = temp
		dummy = cur
		cur = cur.prev
	return dummy


def reverseDLL(head):
	n = head
	while n.next is not None:
		(n.prev, n.next) = (n.next, n.prev)
		n = n.prev
	(n.prev, n.next) = (n.next, n.prev)
	return n


def reverseDLL(head):
	cur = head
	prev = None
	while cur:
		headNext = cur.next
		cur.next = prev
		prev = cur
		cur = headNext
	return prev


def reverseDLL(head):
	if not head:
		return head
	newhead = head
	if head.next:
		newhead = reverseDLL(head.next)
		head.next.next = head
	head.next = None
	return newhead


def reverseDLL(head):
	if head.next is None:
		return head
	newhead = None
	cur = head
	while cur is not None:
		temp = cur.prev
		cur.prev = cur.next
		cur.next = temp
		newhead = cur
		cur = cur.prev
	return newhead


def reverseDLL(head):
	cur = head
	prev = None
	while cur != None:
		s = cur.next
		cur.next = cur.prev
		prev = cur
		cur = s
	return prev


def reverseDLL(head):
	itr = head
	while itr:
		temp = itr.prev
		itr.prev = itr.next
		itr.next = temp
		dummy = itr
		itr = itr.prev
	return dummy


def reverseDLL(head):
	a = []
	while head:
		a.append(head.data)
		head = head.next
	a.reverse()
	s = Node(a[0])
	p = s
	for i in range(1, len(a)):
		t = Node(a[i])
		s.next = t
		t.prev = s
		s = s.next
	return p


def reverseDLL(head):
	prev = None
	curr = head
	nxt = None
	while curr != None:
		nxt = curr.next
		curr.next = prev
		curr.prev = next
		prev = curr
		curr = nxt
	return prev


def reverseDLL(head):
	temp = head
	count = 1
	while temp.next:
		temp = temp.next
		count += 1
	head = temp
	(temp.next, temp.prev) = (temp.prev, temp.next)
	while count != 1:
		count -= 1
		temp = temp.next
		(temp.next, temp.prev) = (temp.prev, temp.next)
	return head


def reverseDLL(head):
	ptr = head
	while ptr:
		temp = ptr.next
		ptr.next = ptr.prev
		ptr.prev = temp
		head = ptr
		ptr = ptr.prev
	return head


def reverseDLL(head):
	temp = head
	prev = None
	while temp != None:
		nxt = temp.next
		temp.next = prev
		temp.prev = nxt
		prev = temp
		temp = nxt
	return prev


def reverseDLL(head):
	if not head.next:
		return head
	cur = head
	while cur:
		temp = cur.prev
		cur.prev = cur.next
		cur.next = temp
		cur = cur.prev
	return temp.prev


def reverseDLL(head):
	cur = tail = head
	n = 0
	while tail.next:
		n += 1
		tail = tail.next
	for _ in range((n + 1) // 2):
		(cur.data, tail.data) = (tail.data, cur.data)
		cur = cur.next
		tail = tail.prev
	return head


def reverseDLL(head):
	curr = head
	while curr:
		tmp = curr.prev
		curr.prev = curr.next
		curr.next = tmp
		dum = curr
		curr = curr.prev
	return dum


def reverseDLL(head):
	start = head
	cur = head
	if head.prev == None and head.next == None:
		return head
	while cur != None:
		temp = cur.prev
		cur.prev = cur.next
		cur.next = temp
		cur = cur.prev
	return temp.prev


def reverseDLL(head):
	start = None
	while head != None:
		temp = head
		head = head.next
		if head:
			head.prev = None
		temp.next = start
		if start != None:
			start.prev = temp
		start = temp
	return start


def reverseDLL(head):
	c = head
	while c:
		temp = c.prev
		c.prev = c.next
		c.next = temp
		d = c
		c = c.prev
	return d


def reverseDLL(head):
	curr = head
	_next = None
	prev = None
	while curr:
		_next = curr.next
		(curr.next, curr.prev) = (curr.prev, curr.next)
		if _next is None:
			prev = curr
		curr = _next
	return prev


def reverseDLL(head):
	if head == None:
		return None
	elif head.next == None:
		return head
	else:
		curr = head
		while curr != None:
			prev = curr
			(curr.prev, curr.next) = (curr.next, curr.prev)
			curr = curr.prev
		return prev


def reverseDLL(head):
	prev = None
	curr = head
	while curr:
		temp_nxt = curr.next
		temp_prev = curr.prev
		curr.next = prev
		curr.prev = temp_nxt
		(prev, curr) = (curr, temp_nxt)
	return prev


def reverseDLL(head_ref):
	current = head_ref
	temp = None
	while current != None:
		temp = current.prev
		current.prev = current.next
		current.next = temp
		current = current.prev
	if temp != None:
		head_ref = temp.prev
	return head_ref