Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor: Method name and inline test calls
  • Loading branch information
10kartik committed Sep 9, 2022
commit fbce118f785773393598b991dc9c9a3d987735a9
4 changes: 2 additions & 2 deletions Data-Structures/Linked-List/SinglyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* a singly linked list.
*/

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

class Node {
constructor (data) {
Expand Down Expand Up @@ -194,7 +194,7 @@ class LinkedList {
}

// Returns a reference to middle node of linked list
MiddleOfLL () {
findMiddle () {
// If there are two middle nodes, return the second middle node.
let fast = this.headNode
let slow = this.headNode
Expand Down
9 changes: 3 additions & 6 deletions Data-Structures/Linked-List/test/SinglyLinkedList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ describe('SinglyLinkedList', () => {
const list = new LinkedList()
list.addFirst(1)

let MiddleNodeOfLinkedList = list.MiddleOfLL(list.headNode)
// Middle node for list having single node
expect(MiddleNodeOfLinkedList.data).toEqual(1)
expect(list.findMiddle().data).toEqual(1)

list.addLast(2)
list.addLast(3)
Expand All @@ -179,15 +178,13 @@ describe('SinglyLinkedList', () => {
list.addLast(6)
list.addLast(7)

MiddleNodeOfLinkedList = list.MiddleOfLL(list.headNode)
// Middle node for list having odd number of nodes
expect(MiddleNodeOfLinkedList.data).toEqual(4)
expect(list.findMiddle().data).toEqual(4)

list.addLast(10)

MiddleNodeOfLinkedList = list.MiddleOfLL(list.headNode)
// Middle node for list having even number of nodes
expect(MiddleNodeOfLinkedList.data).toEqual(5)
expect(list.findMiddle().data).toEqual(5)
})

it('Check Iterator', () => {
Expand Down