Skip to content

Commit dfe200a

Browse files
rahulrajrdgithub-actions
andauthored
merge: Improving coding standard of singly linked list (TheAlgorithms#980)
* Auto-update DIRECTORY.md * Changes on SinglyLinkedList * Spelling change * Added comment for initiateNodeAndIndex() Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 35035f7 commit dfe200a

File tree

2 files changed

+120
-109
lines changed

2 files changed

+120
-109
lines changed

Data-Structures/Linked-List/SinglyLinkedList.js

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* a singly linked list.
77
*/
88

9-
// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, get
9+
// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, get, clean
1010

1111
class Node {
1212
constructor (data) {
@@ -21,112 +21,117 @@ class LinkedList {
2121
this.length = 0
2222
}
2323

24+
// initiates the currentNode and currentIndex and return as an object
25+
initiateNodeAndIndex () {
26+
return { currentNode: this.headNode, currentIndex: 0 }
27+
}
28+
2429
// Returns length
2530
size () {
2631
return this.length
2732
}
2833

2934
// Returns the head
3035
head () {
31-
return this.headNode ? this.headNode.data : null
36+
return this.headNode?.data || null
37+
}
38+
39+
// Return if the list is empty
40+
isEmpty () {
41+
return this.length === 0
3242
}
3343

3444
// add a node at last it to linklist
3545
addLast (element) {
36-
const node = new Node(element)
37-
3846
// Check if its the first element
3947
if (this.headNode === null) {
40-
this.headNode = node
41-
} else {
42-
let currentNode = this.headNode
43-
44-
// Loop till there is a node present in the list
45-
while (currentNode.next) {
46-
currentNode = currentNode.next
47-
}
48+
return this.addFirst(element)
49+
}
50+
let { currentNode } = this.initiateNodeAndIndex()
4851

49-
// Adding node at the end of the list
50-
currentNode.next = node
52+
// Loop till there is a node present in the list
53+
while (currentNode.next) {
54+
currentNode = currentNode.next
5155
}
5256

53-
this.length++ // Increment the length
57+
const node = new Node(element)
58+
// Adding node at the end of the list and increase the length
59+
currentNode.next = node
60+
this.length++
61+
return this.size()
5462
}
5563

5664
// add a node at first it to linklist
5765
addFirst (element) {
5866
const node = new Node(element)
5967
node.next = this.headNode
6068
this.headNode = node
61-
this.length++ // Increment the length
69+
this.length++
70+
return this.size()
6271
}
6372

6473
// remove the first from the linklist
6574
removeFirst () {
75+
const removedNode = this.headNode
6676
if (this.length > 0) {
6777
this.headNode = this.headNode.next
6878
this.length--
6979
}
80+
console.log(removedNode.data)
81+
return removedNode?.data
7082
}
7183

7284
// remove the last from the linklist
7385
removeLast () {
86+
if (this.isEmpty()) return null
7487
if (this.length === 1) {
75-
this.headNode = null
76-
this.length--
77-
} else if (this.length > 1) {
78-
let index = 0
79-
let currentNode = this.headNode
80-
while (index !== this.length - 2) {
81-
index++
82-
currentNode = currentNode.next
83-
}
84-
85-
currentNode.next = null
86-
this.length--
88+
return this.removeFirst()
89+
}
90+
let { currentIndex, currentNode } = this.initiateNodeAndIndex()
91+
while (currentIndex !== this.length - 2) {
92+
currentIndex++
93+
currentNode = currentNode.next
8794
}
95+
const removedNode = currentNode.next
96+
currentNode.next = null
97+
this.length--
98+
return removedNode.data
8899
}
89100

90101
// Removes the node with the value as param
91102
remove (element) {
92-
let currentNode = this.headNode
93-
103+
if (this.isEmpty()) return null
104+
let { currentNode } = this.initiateNodeAndIndex()
105+
let removedNode = null
94106
// Check if the head node is the element to remove
95107
if (currentNode.data === element) {
96-
this.headNode = currentNode.next
97-
} else {
98-
// Check which node is the node to remove
99-
while (currentNode && currentNode.next) {
100-
if (currentNode.next.data === element) {
101-
currentNode.next = currentNode.next.next
102-
break
103-
}
104-
currentNode = currentNode.next
108+
return this.removeFirst()
109+
}
110+
// Check which node is the node to remove
111+
while (currentNode?.next) {
112+
if (currentNode.next.data === element) {
113+
removedNode = currentNode.next
114+
currentNode.next = currentNode.next.next
115+
this.length--
116+
break
105117
}
118+
currentNode = currentNode.next
106119
}
107-
108-
this.length-- // Decrementing the length
109-
}
110-
111-
// Return if the list is empty
112-
isEmpty () {
113-
return this.length === 0
120+
return removedNode?.data || null
114121
}
115122

116123
// Returns the index of the element passed as param otherwise -1
117124
indexOf (element) {
118-
let currentNode = this.headNode
119-
let index = 0
125+
let { currentIndex, currentNode } = this.initiateNodeAndIndex()
120126

121127
while (currentNode) {
122128
// Checking if the node is the element we are searching for
123129
if (currentNode.data === element) {
124-
return index
130+
return currentIndex
125131
}
126132
currentNode = currentNode.next
127-
index++
133+
currentIndex++
128134
}
129-
130135
return -1
131136
}
132137

@@ -135,75 +140,70 @@ class LinkedList {
135140
if (index >= this.length || index < 0) {
136141
throw new RangeError('Out of Range index')
137142
}
138-
let currentNode = this.headNode
139-
let count = 0
140-
while (count < index) {
141-
count++
143+
let { currentIndex, currentNode } = this.initiateNodeAndIndex()
144+
while (currentIndex < index) {
145+
currentIndex++
142146
currentNode = currentNode.next
143147
}
144148
return currentNode.data
145149
}
146150

147151
// Adds the element at specified index
148152
addAt (index, element) {
149-
const node = new Node(element)
150-
151153
// Check if index is out of bounds of list
152154
if (index > this.length || index < 0) {
153155
throw new RangeError('Out of Range index')
154156
}
157+
if (index === 0) return this.addFirst(element)
158+
if (index === this.length) return this.addLast(element)
159+
let { currentIndex, currentNode } = this.initiateNodeAndIndex()
160+
const node = new Node(element)
155161

156-
let currentNode = this.headNode
157-
let currentIndex = 0
158-
// Check if index is the start of list
159-
if (index === 0) {
160-
node.next = currentNode
161-
this.headNode = node
162-
} else {
163-
while (currentIndex !== index - 1) {
164-
currentIndex++
165-
currentNode = currentNode.next
166-
}
167-
168-
// Adding the node at specified index
169-
const temp = currentNode.next
170-
currentNode.next = node
171-
node.next = temp
162+
while (currentIndex !== index - 1) {
163+
currentIndex++
164+
currentNode = currentNode.next
172165
}
173166

167+
// Adding the node at specified index
168+
const tempNode = currentNode.next
169+
currentNode.next = node
170+
node.next = tempNode
174171
// Incrementing the length
175172
this.length++
173+
return this.size()
176174
}
177175

178176
// Removes the node at specified index
179177
removeAt (index) {
180-
let currentNode = this.headNode
181-
let currentIndex = 0
182-
183178
// Check if index is present in list
184179
if (index < 0 || index >= this.length) {
185180
throw new RangeError('Out of Range index')
186181
}
182+
if (index === 0) return this.removeFirst()
183+
if (index === this.length) return this.removeLast()
187184

188-
// Check if element is the first element
189-
if (index === 0) {
190-
this.headNode = currentNode.next
191-
} else {
192-
while (currentIndex !== index - 1) {
193-
currentIndex++
194-
currentNode = currentNode.next
195-
}
196-
currentNode.next = currentNode.next.next
185+
let { currentIndex, currentNode } = this.initiateNodeAndIndex()
186+
while (currentIndex !== index - 1) {
187+
currentIndex++
188+
currentNode = currentNode.next
197189
}
198-
190+
const removedNode = currentNode.next
191+
currentNode.next = currentNode.next.next
199192
// Decrementing the length
200193
this.length--
194+
return removedNode.data
195+
}
196+
197+
// make the linkedList Empty
198+
clean () {
199+
this.headNode = null
200+
this.length = 0
201201
}
202202

203203
// Method to get the LinkedList
204204
get () {
205205
const list = []
206-
let currentNode = this.headNode
206+
let { currentNode } = this.initiateNodeAndIndex()
207207
while (currentNode) {
208208
list.push(currentNode.data)
209209
currentNode = currentNode.next
@@ -214,7 +214,7 @@ class LinkedList {
214214

215215
// Method to iterate over the LinkedList
216216
iterator () {
217-
let currentNode = this.headNode
217+
let { currentNode } = this.initiateNodeAndIndex()
218218
if (currentNode === null) return -1
219219

220220
const iterate = function * () {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy