Skip to content

Commit 3f686f6

Browse files
Added Singly Linked List implementation in Java
1 parent ab5f49e commit 3f686f6

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

.DS_Store

8 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.Slash; // This is basically the folder our code belongs to.
2+
3+
public class LinkedList {
4+
5+
private Node head; // Reference variable pointing to nodes.
6+
private Node tail; // Reference variable pointing to nodes.
7+
private int size; // Number of elements in the linked list.
8+
public LinkedList(){
9+
this.size=0;
10+
}
11+
12+
private class Node {
13+
14+
private int value;
15+
private Node next;
16+
17+
public Node(int value) {
18+
this.value = value;
19+
}
20+
21+
public Node(int value, Node next){
22+
this.value = value;
23+
this.next = next;
24+
}
25+
}
26+
27+
// Inserting a new node at the starting of the list.
28+
public void insertFirst(int val){
29+
Node node = new Node(val);
30+
node.next = head; // Assigning the newly created node to point to the previous head.
31+
head = node; // Changing the head to the current node.
32+
33+
if (tail == null)
34+
tail = head; // If this is the only node in the list, then tail also points to this address.
35+
36+
size += 1; // Increasing the size of the list coz new element added.
37+
}
38+
39+
// Inserting a new node at the end of the list.
40+
public void insertLast(int val){
41+
if (tail == null)
42+
insertFirst(val);
43+
else {
44+
Node node = new Node(val);
45+
tail.next = node;
46+
tail = node;
47+
size += 1;
48+
}
49+
}
50+
51+
// Inserting a new node at any given list
52+
public void insert(int val, int index){
53+
if (index == 0) {
54+
insertFirst(val);
55+
return;
56+
}
57+
58+
if(index == size){
59+
insertLast(val);
60+
return;
61+
}
62+
63+
Node temp = head;
64+
for (int i = 1; i < index; i++) {
65+
temp = temp.next; // Reaching before the node where we want to insert our value.
66+
}
67+
Node node = new Node(val, temp.next); // Pointing the new node to the next value of the temporary node.
68+
temp.next = node;
69+
size++;
70+
}
71+
72+
// Delete node at the first index.
73+
public int deleteFirst(){
74+
int val = head.value;
75+
head = head.next;
76+
if(head==null){
77+
tail = null;
78+
}
79+
size -= 1;
80+
return val;
81+
}
82+
83+
// Delete node at the last index.
84+
public int deleteLast(){
85+
if(size <= 1){
86+
return deleteFirst();
87+
}
88+
89+
Node secondLast = get(size - 2);
90+
int value = tail.value;
91+
tail = secondLast;
92+
tail.next = null;
93+
size -= 1;
94+
return value;
95+
}
96+
97+
// To delete a node at given index.
98+
public int delete(int index){
99+
if (index == 0)
100+
return deleteFirst();
101+
if (index == size-1)
102+
return deleteLast();
103+
Node prev = get(index - 1);
104+
Node deleted = prev.next;
105+
int val = deleted.value;
106+
prev.next = deleted.next;
107+
size -= 1;
108+
return val;
109+
}
110+
111+
// To return a node that has a particular value.
112+
public Node find(int value){
113+
Node node = head;
114+
while(node != null){
115+
if(node.value == value)
116+
return node;
117+
node = node.next;
118+
}
119+
return null;
120+
}
121+
122+
// To return a node at a particular index.
123+
public Node get(int index){
124+
Node node = head;
125+
for (int i = 0; i < index; i++) {
126+
node = node.next;
127+
}
128+
return node;
129+
}
130+
131+
// Displaying the entire linked list.
132+
public void displayList(){
133+
Node temp = head;
134+
while(temp != null) {
135+
System.out.print(temp.value + "-->");
136+
temp = temp.next;
137+
}
138+
System.out.println("END");
139+
}
140+
}

0 commit comments

Comments
 (0)