From a57bc4cb537eaabb32a63964da7dc011184dbc97 Mon Sep 17 00:00:00 2001 From: Varshitha Yedla Date: Thu, 9 Apr 2026 15:30:05 -0700 Subject: [PATCH] Done PreCourse-1 --- Exercise_1.java | 35 ++++++++++++++++++++++++++--------- Exercise_2.java | 36 +++++++++++++++++++++++++++++------- Exercise_3.java | 45 +++++++++++++++++++++++++++------------------ 3 files changed, 82 insertions(+), 34 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..3ee629603 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,35 +1,52 @@ + // Time Complexity : push, pop and peek operations --> O(1) + // Space Complexity : O(n) + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : + class Stack { - //Please read sample.java file before starting. - //Kindly include Time and Space complexity at top of each file + + static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack boolean isEmpty() { - //Write your code here + return top == -1; //return true if stack is empty } Stack() { - //Initialize your constructor + top = -1;//Initialize your constructor } - boolean push(int x) + boolean push(int x) //time: O(1), space: O(1) { - //Check for stack Overflow - //Write your code here + if(top == MAX-1) //top reached last index indicating stack overflow + return false; + a[++top] = x; //increment the top and then store x + return true; } - int pop() + int pop() //time: O(1), space: O(1) { //If empty return 0 and print " Stack Underflow" //Write your code here + if(top == -1) + { + System.out.println("Stack Underflow"); //if stack empty, returns 0 + return 0; + } + + return a[top--]; //returns the top of the stack then decrements top } - int peek() + int peek() //time: O(1), space: O(1) { //Write your code here + if(top == -1) + return -1; + return a[top]; //same as pop but top isn't decremented } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..d7778d7e5 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,6 +1,12 @@ +// Time Complexity : push, pop and peek operations --> O(1) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : + + public class StackAsLinkedList { - StackNode root; + StackNode root; //points to the top of the stack static class StackNode { int data; @@ -8,31 +14,47 @@ static class StackNode { StackNode(int data) { - //Constructor here + this.data = data; + this.next = null;//Constructor here } } public boolean isEmpty() { - //Write your code here for the condition if stack is empty. + return root==null; //Write your code here for the condition if stack is empty. } - public void push(int data) + public void push(int data) //time: O(1), space: O(1) { - //Write code to push data to the stack. + StackNode new_node = new StackNode(data); //creating a new node + new_node.next = root; //new node points to the top of the list + root = new_node; //new node is reassigned as the top } - public int pop() + public int pop() //time: O(1), space: O(1) { //If Stack Empty Return 0 and print "Stack Underflow" //Write code to pop the topmost element of stack. //Also return the popped element + if(root == null) //checks if list is empty + { + System.out.println("Stack Underflow"); + return 0; + } + + int temp = root.data; //saves root data before we unlink it from the list + root = root.next; //moving root to the current top element in stack + return temp; //returning saved value + } - public int peek() + public int peek() //time: O(1), space: O(1) { //Write code to just return the topmost element without removing it. + if(root == null) //checking if there's any elements in the list + return -1; + return root.data; //returning the root node data which is top of the stack } //Driver code diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..9d9aecef1 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,3 +1,8 @@ +// Time Complexity : O(n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : + import java.io.*; // Java program to implement @@ -17,34 +22,38 @@ static class Node { // Constructor Node(int d) { - //Write your code here + this.data = d; + this.next = null; } } // Method to insert a new node - public static LinkedList insert(LinkedList list, int data) + public static LinkedList insert(LinkedList list, int data) //time:O(n), space:O(1) { - // Create a new node with given data - - // If the Linked List is empty, - // then make the new node as head - - // Else traverse till the last node - // and insert the new_node there + Node new_node = new Node(data); //create a new node + if(list.head == null) //checks if list empty. if yes, assigns new node as head + list.head = new_node; + else + { + Node tail = list.head; //else creates a tail node and traverse through the list + while(tail.next != null) + tail = tail.next; - // Insert the new_node at last node - // Return the list by head - + tail.next = new_node; //adds node at the end of the lisr + } + + return list; } // Method to print the LinkedList. - public static void printList(LinkedList list) + public static void printList(LinkedList list) //time:O(n), space:O(1) { - // Traverse through the LinkedList - - // Print the data at current node - - // Go to next node + Node current = list.head; + while(current != null) //current node traverses through the end of the list prints each value + { + System.out.println(current.data + " "); + current = current.next; + } } // Driver code