Let's give it a try! You have a linked list and you have to implement the functionalities push and pop of stack using this given linked list. Your task is to use the class as shown in the comments in the code editor and complete the functions push() and pop() to implement a stack. 
Example 1:
Input: 
push(2)
push(3)
pop()
push(4) 
pop()
Output: 3 4
Explanation: 
push(2)    the stack will be {2}
push(3)    the stack will be {2 3}
pop()      poped element will be 3,
           the stack will be {2}
push(4)    the stack will be {2 4}
pop()      poped element will be 4
Example 2:
Input: 
pop()
push(4)
push(5)
pop()
Output: -1 5
Your Task: You are required to complete two methods push() and pop(). The push() method takes one argument, an integer 'x' to be pushed into the stack and pop() which returns an integer present at the top and popped out from the stack. If the stack is empty then return -1 from the pop() method.
Expected Time Complexity: O(1) for both push() and pop().
Expected Auxiliary Space: O(1) for both push() and pop().
Constraints:
1 <= Q <= 100
1 <= x <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: class MyStack:





    # class StackNode:



    # # Constructor to initialize a node

    # def __init__(self, data):

    #     self.data = data

    #     self.next = None

        

    #Function to push an integer into the stack.

    def push(self, data):



        # Add code here





    #Function to remove an item from top of the stack.

    def pop(self):



        # Add code here



Solutions:
class Node:

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

class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		new = Node(data)
		new.next = self.head
		self.head = new

	def pop(self):
		if self.head is None:
			return -1
		remove = self.head.data
		self.head = self.head.next
		return remove


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		new = StackNode(data)
		new.next = self.head
		self.head = new

	def pop(self):
		if self.head:
			a = self.head.data
			self.head = self.head.next
			return a
		else:
			return -1


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		node = StackNode(data)
		if self.head == None:
			self.head = node
		else:
			node.next = self.head
			self.head = node

	def pop(self):
		temp = self.head
		if temp == None:
			return -1
		else:
			nodeval = self.head.data
			self.head = self.head.next
			return nodeval


class MyStack:

	def __init__(self):
		self.head = StackNode(float('inf'))

	def push(self, data):
		newNode = StackNode(data)
		newNode.next = self.head
		self.head = newNode

	def pop(self):
		if self.head.data == float('inf'):
			return -1
		ret = self.head.data
		self.head = self.head.next
		return ret


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		if self.head is None:
			self.head = StackNode(data)
		else:
			newNode = StackNode(data)
			newNode.next = self.head
			self.head = newNode

	def pop(self):
		if self.head is None:
			return -1
		poppedNode = self.head
		self.head = poppedNode.next
		return poppedNode.data


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		node = StackNode(data)
		node.next = self.head
		self.head = node

	def pop(self):
		if self.head is None:
			return -1
		val = self.head.data
		self.head = self.head.next
		return val


class MyStack(object):

	def __init__(self):
		self.head = None

	def push(self, data):
		node = StackNode(data)
		node.next = self.head
		self.head = node

	def pop(self):
		if not self.head:
			return -1
		value = self.head.data
		self.head = self.head.next
		return value


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		new_node = StackNode(data)
		new_node.next = self.head
		self.head = new_node

	def pop(self):
		if self.head is None:
			return -1
		x = self.head.data
		self.head = self.head.next
		return x


class MyStack:

	def __init__(self):
		self.top = None

	def push(self, data):
		newNode = StackNode(data)
		newNode.next = self.top
		self.top = newNode

	def pop(self):
		if self.top is None:
			return -1
		topElement = self.top
		self.top = self.top.next
		return topElement.data


class MyStack:

	def __init__(self):
		self.top = None

	def push(self, data):
		new_node = StackNode(data)
		new_node.next = self.top
		self.top = new_node

	def pop(self):
		if self.is_empty():
			return -1
		popped_data = self.top.data
		self.top = self.top.next
		return popped_data

	def is_empty(self):
		return self.top is None


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		temp = StackNode(data)
		if not self.head:
			self.head = temp
		else:
			temp.next = self.head
			self.head = temp

	def pop(self):
		temp = self.head
		if not temp:
			return -1
		else:
			x = temp.data
			self.head = self.head.next
			del temp
			return x


import math

class MyStack:

	def __init__(self):
		self.head = None
		self.sz = 0

	def push(self, data):
		self.sz += 1
		temp = StackNode(data)
		if self.head == None:
			self.head = temp
		temp.next = self.head
		self.head = temp

	def pop(self):
		if self.sz == 0:
			return -1
		else:
			x = self.head.data
			self.head = self.head.next
			self.sz -= 1
			return x


class MyStack:

	def __init__(self):
		self.stack = []

	def push(self, x: int) -> None:
		self.stack.append(x)

	def pop(self) -> int:
		if not self.stack:
			return -1
		return self.stack.pop()


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		new_node = StackNode(data)
		new_node.next = self.head
		self.head = new_node

	def pop(self):
		if self.head == None:
			return -1
		else:
			pop_node = self.head.data
			self.head = self.head.next
			return pop_node


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		new = StackNode(data)
		if self.head == None:
			self.head = new
		else:
			new.next = self.head
			self.head = new

	def pop(self):
		ptr = self.head
		if ptr == None:
			return -1
		else:
			x = ptr.data
			self.head = self.head.next
			del ptr
			return x


class StackNode:

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

class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		newNode = StackNode(data)
		if self.head == None:
			self.head = newNode
		else:
			temp = self.head
			while temp.next:
				temp = temp.next
			temp.next = newNode

	def pop(self):
		if self.head == None:
			return -1
		(prev, curr) = (None, self.head)
		while curr.next:
			prev = curr
			curr = curr.next
		n = curr.data
		if prev == None:
			self.head = None
		else:
			prev.next = None
		return n


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		node = StackNode(data)
		temp = self.head
		self.head = node
		node.next = temp

	def pop(self):
		if not self.head:
			return -1
		head_data = self.head.data
		self.head = self.head.next
		return head_data


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		node = StackNode(data)
		node.next = self.head
		self.head = node

	def pop(self):
		if self.head == None:
			return -1
		kevin = self.head.data
		self.head = self.head.next
		return kevin


class Node:

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

class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		node_copy = Node(data)
		node_copy.next = self.head
		self.head = node_copy

	def pop(self):
		if self.head is None:
			return -1
		data_ref = self.head.data
		self.head = self.head.next
		return data_ref


class MyStack:

	def __init__(self):
		self.arr = []

	def push(self, data):
		self.arr.append(data)

	def pop(self):
		if len(self.arr) > 0:
			return self.arr.pop()
		else:
			return -1


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		node = StackNode(data)
		node.next = self.head
		self.head = node

	def pop(self):
		res = -1
		if self.head is not None:
			node = self.head
			res = node.data
			self.head = node.next
			del node
		return res


class MyStack:

	def __init__(self):
		self.head = None

	def isEmpty(self):
		return self.head is None

	def push(self, data):
		temp = StackNode(data)
		temp.next = self.head
		self.head = temp
		return

	def pop(self):
		if self.isEmpty():
			return -1
		temp = self.head.data
		self.head = self.head.next
		return temp


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, el):
		box = StackNode(el)
		box.next = self.head
		self.head = box

	def pop(self):
		if self.head == None:
			return -1
		key = self.head.data
		self.head = self.head.next
		return key


class MyStack:

	def __init__(self):
		self.head = None
		self.top = -1

	def push(self, data):
		node = StackNode(data)
		self.top += 1
		if self.head:
			node.next = self.head
			self.head = node
		else:
			self.head = node

	def pop(self):
		if self.top == -1:
			return -1
		res = self.head.data
		self.head = self.head.next
		self.top -= 1
		return res


class MyStack:

	def __init__(self):
		self.root = None

	def push(self, data):
		node = StackNode(data)
		if self.root == None:
			self.root = node
		else:
			node.next = self.root
			self.root = node

	def pop(self):
		if self.root == None:
			return -1
		else:
			x = self.root
			self.root = self.root.next
			return x.data


class MyStack:

	def __init__(self):
		self.root = None

	def __init__(self):
		self.root = None

	def isEmpty(self):
		return self.root is None

	def push(self, data):
		newNode = StackNode(data)
		newNode.next = self.root
		self.root = newNode
		return

	def pop(self):
		if self.isEmpty():
			return -1
		temp = self.root
		self.root = self.root.next
		popped = temp.data
		return popped


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		temp = StackNode(data)
		temp.next = self.head
		self.head = temp

	def pop(self):
		if self.head == None:
			return -1
		res = self.head.data
		self.head = self.head.next
		return res


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		if self.head:
			node = self.head
			while node.next:
				node = node.next
			node.next = StackNode(data)
		else:
			self.head = StackNode(data)

	def pop(self):
		if self.head:
			node = self.head
			prev = None
			while node.next:
				prev = node
				node = node.next
			if prev:
				prev.next = None
			else:
				self.head = None
			return node.data
		return -1


class MyStack:

	def __init__(self):
		self.top = None

	def push(self, data):
		new = StackNode(data)
		if self.top is None:
			self.top = new
			self.top.next = None
		else:
			new.next = self.top
			self.top = new

	def pop(self):
		if self.top is None:
			return -1
		elif self.top.next is None:
			temp = self.top
			self.top = None
			return temp.data
		else:
			temp = self.top
			self.top = self.top.next
		return temp.data


class Node:

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

class MyStack:

	def __init__(self):
		self.front = None

	def isEmpty(self):
		return not self.front

	def push(self, data):
		temp = Node(data)
		if self.isEmpty():
			self.front = temp
			return
		temp.next = self.front
		self.front = temp

	def pop(self):
		if self.isEmpty():
			return -1
		res = self.front.data
		self.front = self.front.next
		return res


class MyStack:

	def __init__(self):
		self.a = []

	def push(self, data):
		self.a.append(data)

	def pop(self):
		if len(self.a) == 0:
			return -1
		b = self.a.pop()
		return b


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		n = StackNode(data)
		if self.head == None:
			self.head = n
			return
		n.next = self.head.next
		self.head.next = n
		(self.head.data, n.data) = (n.data, self.head.data)

	def pop(self):
		if self.head != None:
			val = self.head.data
			self.head = self.head.next
			return val
		return -1


class MyStack:

	def __init__(self):
		self.head = self.tail = None

	def isempty(self):
		return self.head == None

	def push(self, data):
		temp = StackNode(data)
		if self.head == None:
			self.head = self.tail = temp
			return
		temp.next = self.head
		self.head = temp

	def pop(self):
		if self.isempty():
			return -1
		temp = self.head
		self.head = self.head.next
		temp.next = None
		return temp.data


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		newNode = StackNode(data)
		newNode.next = self.head
		self.head = newNode

	def pop(self):
		if not self.head:
			return -1
		nex = self.head.next
		val = self.head.data
		self.head.next = None
		self.head = nex
		return val


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		node = StackNode(data)
		if not self.head:
			self.head = node
			return
		temp = self.head
		while temp.next:
			temp = temp.next
		temp.next = node

	def pop(self):
		if not self.head:
			return -1
		if not self.head.next:
			val = self.head.data
			self.head = None
			return val
		prev = self.head
		temp = self.head.next
		while temp.next:
			prev = prev.next
			temp = temp.next
		prev.next = None
		return temp.data


class MyStack:

	def __init__(self):
		self.top = None

	def push(self, data):
		new_node = StackNode(data)
		if not self.top:
			self.top = new_node
		else:
			new_node.next = self.top
			self.top = new_node

	def pop(self):
		if not self.top:
			return -1
		data = self.top.data
		self.top = self.top.next
		return data


class MyStack:

	def __init__(self):
		self.s = []

	def push(self, data):
		self.s.append(data)

	def pop(self):
		if len(self.s) == 0:
			return -1
		return self.s.pop()


class MyStack:

	def __init__(self):
		self.head = None

	class Node:

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

	def push(self, item):
		if self.head == None:
			self.head = self.Node(item)
		else:
			node = self.Node(item)
			h = self.head
			while h.next:
				h = h.next
			h.next = node

	def pop(self):
		h = self.head
		if self.head:
			if self.head.next == None:
				h = self.head
				self.head = None
				return h.data
			else:
				while h.next.next:
					h = h.next
				k = h.next
				h.next = None
				return k.data
		else:
			return -1


class Node:

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

class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		n = Node(data)
		if not self.head:
			self.head = n
		else:
			n.next = self.head
			self.head = n

	def pop(self):
		if not self.head:
			return -1
		else:
			result = self.head.data
			self.head = self.head.next
			return result


class MyStack:

	def __init__(self):
		self.root = None

	def isEmpty(self):
		return True if self.root is None else False

	def push(self, data):
		newNode = StackNode(data)
		newNode.next = self.root
		self.root = newNode

	def pop(self):
		if self.isEmpty():
			return -1
		temp = self.root
		self.root = self.root.next
		popped = temp.data
		return popped

	def peek(self):
		if self.isEmpty():
			return float('-inf')
		return self.root.data


class MyStack:

	def __init__(self):
		self.top = None

	def push(self, data):
		newNode = StackNode(data)
		ptr = self.top
		if ptr is None:
			self.top = newNode
		else:
			newNode.next = self.top
			self.top = newNode
		return self.top

	def pop(self):
		ptr = self.top
		if ptr is None:
			return -1
		popped_val = ptr.data
		self.top = ptr.next
		return popped_val


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		n = StackNode(data)
		if self.head == None:
			self.head = n
		else:
			n.next = self.head
			self.head = n

	def pop(self):
		p = self.head
		if p == None:
			return -1
		else:
			x = p.data
			self.head = self.head.next
			del p
			return x


class MyStack:

	def __init__(self):
		self.h = None
		self.t = None

	def push(self, data):
		if self.h == None and self.t == None:
			nn = StackNode(data)
			self.h = nn
			self.t = nn
		else:
			nn = StackNode(data)
			self.t.next = nn
			self.t = nn

	def pop(self):
		s = self.h
		if s == None:
			return -1
		if self.h == self.t:
			se = self.h.data
			self.h = None
			self.t = None
			return se
		while s and s.next and s.next.next:
			s = s.next
		val = self.t.data
		if s:
			s.next = None
		self.t = self.h
		while self.t and self.t.next:
			self.t = self.t.next
		return val


class MyStack:

	def __init__(self):
		self.head = None
		self.s = 0

	def push(self, data):
		newest = StackNode(data)
		if self.head == None:
			self.head = newest
			self.s += 1
		else:
			newest.next = self.head
			self.head = newest
			self.s += 1

	def pop(self):
		if self.head == None:
			return -1
		x = self.head.data
		self.head = self.head.next
		return x


class MyStack:

	def __init__(self):
		self.head = None
		self.count = 0

	def push(self, data):
		newNode = StackNode(data)
		newNode.next = self.head
		self.head = newNode
		self.count += 1

	def pop(self):
		if self.head is None:
			return -1
		x = self.head.data
		self.head = self.head.next
		self.count -= 1
		return x


class MyStack:

	def __init__(self):
		self.top = None

	def push(self, data):
		if self.top == None:
			self.top = StackNode(data)
			return
		added = StackNode(data)
		added.next = self.top
		self.top = added

	def pop(self):
		if self.top == None:
			return -1
		ret = self.top.data
		self.top = self.top.next
		return ret


class MyStack:

	def __init__(self):
		self.head = None

	def push(self, data):
		if self.head is None:
			self.head = StackNode(data)
		else:
			new_data = StackNode(data)
			new_data.next = self.head
			self.head = new_data

	def pop(self):
		curr1 = self.head
		if curr1 is None:
			return -1
		else:
			prev = self.head
			if prev is None:
				return -1
			else:
				self.head = self.head.next
			return prev.data