You are given the head of a Linked List. You have to move the last element to the front of the Linked List and return the list.
 
Example 1:
Input:
N = 5
List = {2,5,6,2,1}
Output:
{1,2,5,6,2}
Explanation:
In the given linked list, the last element is 1,
after moving the last element to the front the
linked list will be {1,2,5,6,2}.
 
Example 2:
Input:
N = 1
List = {2}
Output:
{2}
Explanation:
Here 2 is the only element so, the linked list
will remain the same.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function moveToFront() which takes the address of the head of the linked list and returns the modified linked list.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{5}
0 <= Elements of List <= 10^{9}
Sum of N over all test cases doesn't exceeds 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: from typing import Optional



"""



Definition for singly Link List Node

class Node:

    def __init__(self,x):

        self.data=x

        self.next=None



You can also use the following for printing the link list.

printList(node)

"""



class Solution:

    def moveToFront(self, head : Optional['Node']) -> Optional['Node']:

        # code here

        



Solutions:
from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head is None:
			return None
		if head.next is None:
			return head
		curr = head
		prev = None
		while curr.next is not None:
			prev = curr
			curr = curr.next
		curr.next = head
		head = curr
		prev.next = None
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head.next == None:
			return head
		h = head
		while h.next:
			prev = h
			h = h.next
		prev.next = None
		h.next = head
		return h


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head == None or head.next == None:
			return head
		firstNode = head
		while head.next.next != None:
			head = head.next
		beforeLastNode = head
		lastNode = head.next
		lastNode.next = firstNode
		beforeLastNode.next = None
		return lastNode


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if not head or not head.next:
			return head
		slow = head
		fast = head
		while fast.next:
			slow = fast
			fast = fast.next
		fast.next = head
		slow.next = None
		head = fast
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		curr_node = head
		prev_node = None
		if head.next is None:
			return head
		while curr_node.next:
			prev_node = curr_node
			curr_node = curr_node.next
		prev_node.next = None
		curr_node.next = head
		head = curr_node
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head == None or head.next == None:
			return head
		curr = head
		while curr.next.next != None:
			curr = curr.next
		last = curr.next
		last.next = head
		curr.next = None
		return last


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if not head:
			return None
		if not head.next:
			return head
		temp = head
		p = head.next
		while p.next:
			temp = temp.next
			p = p.next
		p.next = head
		temp.next = None
		head = p
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		ans = []
		while head:
			ans.append(head.data)
			head = head.next
		c = ans.pop()
		ans.insert(0, c)
		for i in ans:
			print(i, end=' ')


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		cur = head
		if head.next == None:
			return head
		while cur.next.next is not None:
			cur = cur.next
		nodeToBePlaced = cur.next
		cur.next = None
		nodeToBePlaced.next = head
		head = nodeToBePlaced
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head == None or head.next == None:
			return head
		temp = head
		while temp.next and temp.next.next:
			temp = temp.next
		last_node = temp.next
		temp.next = None
		last_node.next = head
		head = last_node
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head.next == None:
			return head
		n = head
		while n.next is not None:
			a = n
			n = n.next
		a.next = None
		n.next = head
		head = n
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head is None or head.next is None:
			return head
		else:
			ptr = head
			while ptr.next:
				prev = ptr
				ptr = ptr.next
			prev.next = None
			ptr.next = head
			head = ptr
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		cur = head
		last = None
		if head.next == None:
			return head
		while cur:
			if cur.next.next == None:
				temp = cur.next
				cur.next = None
				temp.next = head
				break
			cur = cur.next
		if temp:
			return temp
		else:
			return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		prev = head
		itr = head
		while itr.next:
			prev = itr
			itr = itr.next
		itr.next = head
		prev.next = None
		return itr


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head == None:
			return
		elif head.next == None:
			return head
		else:
			temp = head
			prev_temp = temp
			while temp.next:
				prev_temp = temp
				temp = temp.next
			prev_temp.next = None
			temp.next = head
			head = temp
			return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		n = head
		p = None
		nn = head
		pp = None
		c = 1
		while n.next:
			if c == 2:
				pp = n
			p = n
			if n.next and n.next.next == None:
				i = n
			c += 1
			n = n.next
		if p == None:
			return nn
		p.next = None
		n.next = nn
		return n


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		while head == None:
			return None
		while head.next == None:
			return head
		node = head
		node1 = head.next
		while node1.next:
			node1 = node1.next
			node = node.next
		key = node1.data
		new_node = Node(key)
		node.next = None
		new_node.next = head
		head = new_node
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if not head.next:
			return head
		tmp = head
		while head.next.next:
			head = head.next
		front = head.next
		head.next = None
		front.next = tmp
		return front


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head is None or head.next is None:
			return head
		prev = None
		temp1 = None
		temp = head
		while temp is not None:
			temp1 = prev
			prev = temp
			temp = temp.next
		temp1.next = temp
		prev.next = head
		return prev


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		p = head
		if head.next == None:
			return head
		while head.next.next != None:
			head = head.next
		c = head.next
		q = head.next.next
		head.next = None
		q = p
		head = c
		head.next = p
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head.next == None:
			return head
		temp1 = head
		temp2 = head.next
		while temp2.next != None:
			temp1 = temp1.next
			temp2 = temp2.next
		temp2.next = head
		temp1.next = None
		return temp2


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head.next == None:
			return head
		elif head == None:
			return None
		else:
			x = head.next
			y = head
			while x.next != None:
				x = x.next
				y = y.next
			y.next = None
			x.next = head
			return x


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		temp = head
		a = head
		c = 0
		while temp.next:
			c = c + 1
			prev = temp
			temp = temp.next
		if c == 0:
			return head
		prev.next = None
		temp.next = a
		return temp


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if not head.next:
			return head
		ptr = head
		while ptr.next.next:
			ptr = ptr.next
		nh = ptr.next
		ptr.next = None
		nh.next = head
		return nh


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		ptr = head
		prev = ptr
		prev1 = prev
		while ptr:
			prev1 = prev
			prev = ptr
			ptr = ptr.next
		prev.next = head
		prev1.next = None
		return prev


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head.next == None:
			return head
		else:
			stack = [head]
			order = [head]
			while len(stack) > 0:
				current = stack.pop()
				if current.next:
					stack.append(current.next)
					order.append(current.next)
				else:
					current.next = head
					order[-2].next = None
			return current


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head.next == None:
			return head
		t1 = head
		t2 = head.next
		while t2.next != None:
			t1 = t1.next
			t2 = t2.next
		t2.next = head
		t1.next = None
		head = t2
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		l = []
		temp = head
		while temp:
			l.append(temp.data)
			temp = temp.next
		l = l[-1:] + l[:-1]
		for i in l:
			print(i, end=' ')


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head == None or head.next == None:
			return head
		elif head.next.next == None:
			temp = head
			prev = temp.next
			prev.next = head
			head = prev
			temp.next = None
		else:
			temp = head
			while temp.next.next != None:
				temp = temp.next
			curNode = temp
			temp = temp.next
			temp.next = head
			head = temp
			curNode.next = None
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		head1 = head
		value = None
		if head.next == None:
			return head
		while head:
			if head.next.next == None:
				value = head.next.data
				head.next = None
			head = head.next
		head11 = Node(value)
		head = head11
		head.next = head1
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if not head or not head.next:
			return head
		s = head
		while s.next.next:
			s = s.next
		tar = s.next
		s.next = None
		ne = Node(tar.data)
		ne.next = head
		head = ne
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head.next == None:
			return head
		cur = head
		while cur.next.next:
			cur = cur.next
		x = Node(cur.next.data)
		cur.next = None
		x.next = head
		head = x
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head.next == None:
			return head
		temp = head
		while temp.next.next:
			temp = temp.next
		ref = temp.next.data
		temp.next = None
		new_node = Node(ref)
		new_node.next = head
		head = new_node
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		i = 0
		j = 0
		k = 0
		l = 0
		p = head
		if head.next == None:
			return head
		while head.next != None:
			k = head
			head = head.next
		head.next = p
		k.next = None
		return head


from typing import Optional

class Node:

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

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		itr = head
		prev = None
		if head.next:
			while itr.next:
				prev = itr
				itr = itr.next
			itr.next = head
			head = itr
			prev.next = None
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		n = head
		while n.next != None and n.next.next != None:
			n = n.next
		m = n.next
		if m == None:
			return head
		n.next = None
		m.next = head
		head = m
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head.next == None:
			return head
		dummy = Node(0)
		dummy.next = head
		prev = dummy
		trav = head
		while trav.next:
			prev = trav
			trav = trav.next
		prev.next = None
		trav.next = head
		head = trav
		return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		if head == None or head.next == None:
			return head
		temp = head
		while temp:
			if temp.next.next == None:
				ans = Node(temp.next.data)
				temp.next = None
				ans.next = head
				return ans
			temp = temp.next


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		head1 = head
		count = 1
		while head1.next != None:
			prev = head1
			head1 = head1.next
			count += 1
		if count == 1:
			return head
		a = Node(head1.data)
		prev.next = None
		a.next = head
		return a


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		temp = head
		if temp.next == None:
			return head
		else:
			while temp.next.next:
				temp = temp.next
			p = temp.next.data
			temp.next = None
			newnode = Node(p)
			newnode.next = head
			head = newnode
			return head


from typing import Optional

class Solution:

	def moveToFront(self, head: Optional['Node']) -> Optional['Node']:
		cur = head
		if cur.next is None or cur is None:
			return head
		while cur.next:
			temp = cur
			cur = cur.next
		temp.next = None
		cur.next = head
		head = cur
		return head