mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
algorithm: find the middle of linked-list (#1096)
* Added Middle of linked-list implementation. * Added Middle of LL function and tests * Refactor: Added method in singly LL and its tests * Refactor: Method name and inline test calls * Use `!== null` instead of `!= null` Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@@ -164,6 +164,29 @@ describe('SinglyLinkedList', () => {
|
||||
expect(list.size()).toBe(1)
|
||||
})
|
||||
|
||||
it('Middle node of linked list', () => {
|
||||
const list = new LinkedList()
|
||||
list.addFirst(1)
|
||||
|
||||
// Middle node for list having single node
|
||||
expect(list.findMiddle().data).toEqual(1)
|
||||
|
||||
list.addLast(2)
|
||||
list.addLast(3)
|
||||
list.addLast(4)
|
||||
list.addLast(5)
|
||||
list.addLast(6)
|
||||
list.addLast(7)
|
||||
|
||||
// Middle node for list having odd number of nodes
|
||||
expect(list.findMiddle().data).toEqual(4)
|
||||
|
||||
list.addLast(10)
|
||||
|
||||
// Middle node for list having even number of nodes
|
||||
expect(list.findMiddle().data).toEqual(5)
|
||||
})
|
||||
|
||||
it('Check Iterator', () => {
|
||||
const list = new LinkedList()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user