Given a linked list of N nodes. The task is to reverse this list.
Example 1:
Input:
LinkedList: 1->2->3->4->5->6
Output: 6 5 4 3 2 1
Explanation: After reversing the list, 
elements are 6->5->4->3->2->1.
Example 2:
Input:
LinkedList: 2->7->8->9->10
Output: 10 9 8 7 2
Explanation: After reversing the list,
elements are 10->9->8->7->2.
Your Task:
The task is to complete the function reverseList() with head reference as the only argument and should return new head after reversing the list.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #function Template for python3



"""

# Node Class



class node:

    def __init__(self, val):

        self.data = val

        self.next = None



"""



class Solution:

    #Function to reverse a linked list.

    def reverseList(self, head):

        # Code here





Solutions:
class Solution:

	def reverseList(self, head):
		temp = head
		prev = None
		while temp:
			succ = temp.next
			temp.next = prev
			prev = temp
			temp = succ
		return prev


class Solution:

	def reverseList(self, head):
		cur = head
		prev = None
		nxt = head.next
		while cur != None:
			nxt = cur.next
			cur.next = prev
			prev = cur
			cur = nxt
		head = prev
		return head


class Solution:

	def reverseList(self, head):
		curr = head
		next = None
		prev = None
		while curr:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		return prev


class Solution:

	def reverseList(self, head):
		l = []
		temp = head
		while temp != None:
			l.append(temp.data)
			temp = temp.next
		cur = head
		while cur != None:
			cur.data = l.pop()
			cur = cur.next
		return head


class Solution:

	def reverseList(self, head):
		prev = None
		curr = head
		forward = curr.next
		while curr is not None:
			curr.next = prev
			prev = curr
			curr = forward
			if forward is not None:
				forward = forward.next
		head = prev
		return head


class Solution:

	def reverseList(self, head):
		cur = head
		prev = None
		while cur is not None:
			ne = cur.next
			cur.next = prev
			prev = cur
			cur = ne
		return prev


class Solution:

	def reverseList(self, head):
		curr = head
		res = []
		while curr:
			res.append(curr.data)
			curr = curr.next
		curr = head
		while curr:
			curr.data = res.pop()
			curr = curr.next
		return head


class Solution:

	def reverseList(self, head):
		prev = None
		current = head
		while current:
			next = current.next
			current.next = prev
			prev = current
			current = next
		return prev


class Solution:

	def reverseList(self, head):
		prev = None
		curr = head
		while curr is not None:
			next_node = curr.next
			curr.next = prev
			prev = curr
			curr = next_node
		return prev


class Solution:

	def reverseList(self, head):
		l = []
		a = head
		while a != None:
			l.append(a.data)
			a = a.next
		a = head
		while a != None:
			a.data = l.pop()
			a = a.next
		return head


class Solution:

	def reverseList(self, head):
		if head is None or head.next is None:
			return head
		prev = None
		curr = head
		forwerd = None
		while curr != None:
			forwerd = curr.next
			curr.next = prev
			prev = curr
			curr = forwerd
		return prev


class Solution:

	def reverseList(self, head):
		t = head
		s = []
		while t is not None:
			s.append(t.data)
			t = t.next
		s = s[::-1]
		t = Node(s[0])
		ans = t
		if len(s) > 1:
			for x in s[1:]:
				t.next = Node(x)
				t = t.next
		return ans


class Solution:

	def reverseList(self, head):
		ress = []
		curr = head
		while curr != None:
			ress.append(curr.data)
			curr = curr.next
		curr = head
		while curr != None:
			curr.data = ress.pop()
			curr = curr.next
		return head


class Solution:

	def reverseList(self, head):
		if head is None:
			return head
		(prev, curr, next_) = (None, head, None)
		while curr:
			next_ = curr.next
			curr.next = prev
			prev = curr
			curr = next_
		return prev


class Solution:

	def reverseList(self, head):
		prev = None
		current = head
		while current is not None:
			temp = current.next
			current.next = prev
			prev = current
			current = temp
		head = prev
		return head


class Solution:

	def reverseList(self, head):
		prev = None
		curr = head
		nxt = head.next
		while curr != None:
			curr.next = prev
			prev = curr
			curr = nxt
			if curr == None:
				nxt = None
			else:
				nxt = curr.next
		head = prev
		return head


class Solution:

	def reverseList(self, head):
		previous = None
		current = head
		while current:
			next = current.next
			current.next = previous
			previous = current
			current = next
		return previous


import sys

class Solution:

	def recRev(self, p, c):
		if c is None:
			return p
		n = c.next
		c.next = p
		return self.recRev(c, n)

	def reverseList(self, head):
		return self.recRev(None, head)
sys.setrecursionlimit(int(1000000.0))


class Solution:

	def reverseList(self, head):
		(p, c) = (None, head)
		while c:
			n = c.next
			c.next = p
			(p, c) = (c, n)
		return p


class Solution:

	def reverseList(self, head):
		temp = head
		poin = None
		while temp:
			pre = temp.next
			temp.next = poin
			poin = temp
			temp = pre
		return poin


class Solution:

	def reverseList(self, head):
		cu = head
		prev = None
		while cu is not None:
			next = cu.next
			cu.next = prev
			prev = cu
			cu = next
		head = prev
		return head


class Solution:

	def reverseList(self, head):
		if head == None and head.next == None:
			return head
		prev = None
		curr = head
		forward = None
		while curr != None:
			forward = curr.next
			curr.next = prev
			prev = curr
			curr = forward
		return prev


class node:

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

class Solution:

	def reverseList(self, head):
		prev = None
		current = head
		while current is not None:
			next = current.next
			current.next = prev
			prev = current
			current = next
		head = prev
		return head


import sys

class Solution:

	def reverse(self, prev, curr):
		if curr == None:
			return prev
		head = self.reverse(curr, curr.next)
		curr.next = prev
		return head

	def reverseList(self, head):
		sys.setrecursionlimit(100000)
		prev = None
		curr = head
		return self.reverse(prev, curr)


class node:

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

class Solution:

	def reverseList(self, head):
		prev = None
		if head is not None:
			cur = head
		else:
			return head
		while True:
			ne = cur.next
			cur.next = prev
			prev = cur
			if ne is not None:
				cur = ne
			else:
				return cur
		return cur


class Solution:

	def reverseList(self, head):
		if head == None:
			return head
		if head.next == None:
			return head
		r = head
		head = head.next
		r.next = None
		while head != None:
			temp = r
			r = head
			head = head.next
			r.next = None
			r.next = temp
		return r


class Node:

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

class Solution:

	def reverseList(self, head):
		prev = None
		curr = head
		next = None
		while curr:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		head = prev
		return head


class Solution:

	def reverseList(self, head):
		curr = head
		later = None
		while curr:
			next = curr.next
			curr.next = later
			later = curr
			curr = next
		return later


class Solution:

	def reverseList(self, head):
		curr_node = head
		prev_node = None
		while curr_node:
			next_node = curr_node.next
			curr_node.next = prev_node
			prev_node = curr_node
			curr_node = next_node
		head = prev_node
		return head


class Solution:

	def reverseList(self, head):
		temp_lst = []
		curr_node = head
		while curr_node:
			temp_lst.append(curr_node.data)
			curr_node = curr_node.next
		counter = -1
		x = len(temp_lst)
		curr_node = head
		for i in range(-1, -(x + 1), -1):
			curr_node.data = temp_lst[i]
			curr_node = curr_node.next
		return head


class Solution:

	def reverseList(self, head):
		k = []
		temp = head
		while temp != None:
			k.append(temp.data)
			temp = temp.next
		temp = head
		while temp != None:
			temp.data = k.pop()
			temp = temp.next
		return head


class node:

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

class Solution:

	def reverseList(self, head):
		prev = None
		current = head
		while current is not None:
			next = current.next
			current.next = prev
			prev = current
			current = next
		self.head = prev
		temp = self.head
		while temp:
			print(temp.data, end=' ')
			temp = temp.next


class Solution:

	def reverseList(self, head):
		x = []
		while head:
			x.append(head.data)
			head = head.next
		x = x[::-1]
		head = Node(x[0])
		cur = head
		for i in range(1, len(x)):
			cur.next = Node(x[i])
			cur = cur.next
		return head


class Solution:

	def reverseList(self, head):
		prev = None
		while head:
			ahead = head.next
			head.next = prev
			prev = head
			head = ahead
		return prev


class Solution:

	def reverseList(self, head):
		l = None
		p = head
		while p:
			temp = Node(p.data)
			if l is None:
				l = temp
			else:
				temp.next = l
				l = temp
			p = p.next
		return l


class Solution:

	def reverseList(self, head):
		if not head or not head.next:
			return head
		p = None
		c = head
		n = head.next
		while n:
			c.next = p
			p = c
			c = n
			n = n.next
		c.next = p
		return c


import sys
sys.setrecursionlimit(550000)

class Solution:

	def reverseList(self, head):
		curr = None
		rem = head

		def helper(curr, node):
			if not node:
				return curr
			t = node.next
			node.next = curr
			return helper(node, t)
		return helper(curr, rem)


class Solution:

	def reverseList(self, head):
		dummy = None
		while head:
			nextnode = head.next
			head.next = dummy
			dummy = head
			head = nextnode
		return dummy


class Solution:

	def reverseList(self, head):
		if head is None:
			return
		prev = None
		current = head
		while current is not None:
			nextnode = current.next
			current.next = prev
			prev = current
			current = nextnode
		return prev


class Solution:

	def reverseList(self, head):
		temp = None
		while head:
			next = head.next
			head.next = temp
			temp = head
			head = next
		return temp