Skip to content

Commit b246813

Browse files
#982 solved
1 parent c22c78f commit b246813

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Linked List/singly_linked_list.java

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Linked list: Implement Singly linked list in Java #982
2+
3+
/*
4+
APPROACH :Node Class: The Node class is defined to represent a single node in the linked list. It has two instance variables: data to store the value and next to store the reference to the next node.
5+
6+
Insertion at the Start: The insertStart method allows inserting a new node at the beginning of the linked list. It creates a new node with the given data value and sets its next reference to the current head. Then, it updates the head to point to the newly inserted node.
7+
8+
Deletion from the Start: The delete method removes the first node from the linked list. It checks if the head is null, indicating an empty list. If not, it updates the head to point to the next node, effectively removing the first node.
9+
10+
Display: The display method is used to print the elements of the linked list. It starts from the head node and iterates through the list by moving to the next node until the current node becomes null. During each iteration, it prints the data value of the current node.
11+
12+
Main Method: The main method demonstrates the usage of the linked list implementation. It creates an empty list by initializing the head to null. It then performs several insertions using the insertStart method to add nodes at the beginning of the list. After that, it calls the display method to print the elements of the list. Next, it performs several deletions using the delete method to remove nodes from the beginning of the list. Finally, it calls the display method again to print the updated list.
13+
*/
14+
15+
import java.lang.*;
16+
17+
// Node Class
18+
class Node {
19+
int data;
20+
Node next;
21+
22+
Node(int x) // parameterized constructor
23+
{
24+
data = x;
25+
next = null;
26+
}
27+
}
28+
29+
class Main
30+
{
31+
static Node insertStart(Node head, int data)
32+
{
33+
// Creating newNode memory & assigning data value
34+
Node newNode = new Node(data);
35+
36+
// assigning this newNode's next as current head node
37+
newNode.next = head;
38+
// re-assigning head to this newNode
39+
head = newNode;
40+
41+
return head;
42+
}
43+
44+
public static Node delete(Node head)
45+
{
46+
if (head == null){
47+
System.out.println("List is empty, not possible to delete");
48+
return head;
49+
}
50+
51+
System.out.println("Deleted: " + head.data);
52+
// move head to next node
53+
head = head.next;
54+
55+
return head;
56+
}
57+
58+
static void display(Node node) {
59+
60+
//as linked list will end when Node is Null
61+
while (node != null) {
62+
System.out.print(node.data + " ");
63+
node = node.next;
64+
}
65+
System.out.println("");
66+
}
67+
68+
public static void main(String args[])
69+
{
70+
Node head = null;
71+
head = insertStart(head,6);
72+
head = insertStart(head,5);
73+
head = insertStart(head,4);
74+
head = insertStart(head,3);
75+
head = insertStart(head,2);
76+
head = insertStart(head,1);
77+
78+
display(head);
79+
80+
head = delete(head);
81+
head = delete(head);
82+
head = delete(head);
83+
84+
display(head);
85+
86+
87+
}
88+
}
89+
90+
/*
91+
the space complexity is O(n), and the time complexity for each operation is either O(1) or O(n), depending on the specific operation being performed.
92+
*/

0 commit comments

Comments
 (0)