Given a circular linked list, your task is to complete the method printList() that prints the linked list.
Input:
The printList function takes a single argument as input the reference pointer to the head of the linked list.
There are multiple test cases and for each test, the function will be called separately. 
Output: You just need to print the LinkedList in the same line and the next line will be added by the Driver Code.
Example:
Input:
2
7
374 363 171 497 282 306 426
2
162 231
Output:
426 306 282 497 171 363 374
231 162
Note : Input items are inserted at the front of linked list that is why output is in reverse order.
Constraints:
1<=T<=50
1<=N<=50

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: class Node: 

      

    # Constructor to create  a new node 

    def __init__(self, data): 

        self.data = data  

        self.next = None

  

class CircularLinkedList: 

      

    # Constructor to create a empty circular linked list 

    def __init__(self): 

        self.head = None

  

    # Function to insert a node at the beginning of a 

    # circular linked list 

    def push(self, data): 

        ptr1 = Node(data)

        ptr1.data=data

        temp = self.head 

          

        ptr1.next = self.head 

  

        # If linked list is not None then set the next of 

        # last node 

        if self.head is not None: 

            while(temp.next != self.head): 

                temp = temp.next 

            temp.next = ptr1 

  

        else: 

            ptr1.next = ptr1 # For the first node 

  

        self.head = ptr1  

  

    # Function to print nodes in a given circular linked list 

    def printList(self): 

        

        # Write your code here

        return

        

Solutions:
class Node:

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

class CircularLinkedList:

	def __init__(self):
		self.head = None

	def push(self, data):
		ptr1 = Node(data)
		ptr1.data = data
		temp = self.head
		ptr1.next = self.head
		if self.head is not None:
			while temp.next != self.head:
				temp = temp.next
			temp.next = ptr1
		else:
			ptr1.next = ptr1
		self.head = ptr1

	def printList(self):
		temp = self.head
		while temp:
			print(temp.data, end=' ')
			temp = temp.next
			if temp == self.head:
				break
		return


class Node:

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

class CircularLinkedList:

	def __init__(self):
		self.head = None

	def push(self, data):
		if self.head is None:
			self.head = Node(data)
		else:
			p = self.head
			while p.next:
				p = p.next
			p.next = Node(data)

	def printList(self):
		head = self.head
		l = []
		while head:
			l.append(head.data)
			head = head.next
		print(*l[::-1], end='')


class Node:

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

class CircularLinkedList:

	def __init__(self):
		self.head = None

	def push(self, data):
		ptr1 = Node(data)
		ptr1.data = data
		temp = self.head
		ptr1.next = self.head
		if self.head is not None:
			while temp.next != self.head:
				temp = temp.next
			temp.next = ptr1
		else:
			ptr1.next = ptr1
		self.head = ptr1

	def printList(self):
		itr = self.head
		flag = 0
		while itr:
			if itr == self.head and flag == 1:
				break
			print(itr.data, end=' ')
			itr = itr.next
			flag = 1
		return


class Node:

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

class CircularLinkedList:

	def __init__(self):
		self.head = None

	def push(self, data):
		ptr1 = Node(data)
		ptr1.data = data
		temp = self.head
		ptr1.next = self.head
		if self.head is not None:
			while temp.next != self.head:
				temp = temp.next
			temp.next = ptr1
		else:
			ptr1.next = ptr1
		self.head = ptr1

	def printList(self):
		current = self.head
		seen = set()
		while current not in seen:
			print(current.data, end=' ')
			seen.add(current)
			current = current.next
		return


class Node:

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

class CircularLinkedList:

	def __init__(self):
		self.head = None

	def push(self, data):
		ptr1 = Node(data)
		ptr1.data = data
		temp = self.head
		ptr1.next = self.head
		if self.head is not None:
			while temp.next != self.head:
				temp = temp.next
			temp.next = ptr1
		else:
			ptr1.next = ptr1
		self.head = ptr1

	def printList(self):
		curr = self.head
		if self.head == None:
			return 0
		else:
			count = 1
			print(curr.data, end=' ')
			curr = curr.next
			while curr != self.head:
				print(curr.data, end=' ')
				curr = curr.next


class Node:

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

class CircularLinkedList:

	def __init__(self):
		self.head = None

	def push(self, data):
		ptr1 = Node(data)
		ptr1.data = data
		temp = self.head
		ptr1.next = self.head
		if self.head is not None:
			while temp.next != self.head:
				temp = temp.next
			temp.next = ptr1
		else:
			ptr1.next = ptr1
		self.head = ptr1

	def printList(self):
		x = []
		cur = self.head
		temp = self.head
		x.append(temp.data)
		temp = temp.next
		while temp != cur:
			x.append(temp.data)
			temp = temp.next
		for i in x:
			print(i, end=' ')
		return


class Node:

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

class CircularLinkedList:

	def __init__(self):
		self.head = None

	def push(self, data):
		ptr1 = Node(data)
		ptr1.data = data
		temp = self.head
		ptr1.next = self.head
		if self.head is not None:
			while temp.next != self.head:
				temp = temp.next
			temp.next = ptr1
		else:
			ptr1.next = ptr1
		self.head = ptr1

	def printList(self):
		h = self.head
		ans = []
		while h.next is not self.head:
			ans.append(h.data)
			h = h.next
		ans.append(h.data)
		for i in ans:
			print(i, end=' ')


class Node:

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

class CircularLinkedList:

	def __init__(self):
		self.head = None

	def push(self, data):
		ptr1 = Node(data)
		ptr1.data = data
		temp = self.head
		ptr1.next = self.head
		if self.head is not None:
			while temp.next != self.head:
				temp = temp.next
			temp.next = ptr1
		else:
			ptr1.next = ptr1
		self.head = ptr1

	def printList(self):
		head = self.head
		n = head
		c = 0
		while n.next != head:
			c += 1
			n = n.next
		c += 1
		n = head
		while c > 0:
			print(n.data, end=' ')
			n = n.next
			c -= 1
		return
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		cllist = CircularLinkedList()
		l = list(map(int, input().strip().split()))
		for i in range(0, len(l)):
			cllist.push(l[i])
		cllist.printList()
		print()