GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithm: find the middle of linked-list (#1096) · Algorithms-Learn/JavaScript@6ad2467

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Data-Structures/Linked-List
    • SinglyLinkedList.js
    • test
      • SinglyLinkedList.test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Data-Structures/Linked-List/SinglyLinkedList.js‎

Copy file name to clipboardExpand all lines: Data-Structures/Linked-List/SinglyLinkedList.js
+14-1Lines changed: 14 additions & 1 deletion
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, clean
9+
// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, findMiddle, get, clean
1010

1111
class Node {
1212
constructor (data) {
@@ -193,6 +193,19 @@ class LinkedList {
193193
return removedNode.data
194194
}
195195

196+
// Returns a reference to middle node of linked list
197+
findMiddle () {
198+
// If there are two middle nodes, return the second middle node.
199+
let fast = this.headNode
200+
let slow = this.headNode
201+
202+
while (fast !== null && fast.next !== null) {
203+
fast = fast.next.next
204+
slow = slow.next
205+
}
206+
return slow
207+
}
208+
196209
// make the linkedList Empty
197210
clean () {
198211
this.headNode = null
Collapse file

‎Data-Structures/Linked-List/test/SinglyLinkedList.test.js‎

Copy file name to clipboardExpand all lines: Data-Structures/Linked-List/test/SinglyLinkedList.test.js
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,29 @@ describe('SinglyLinkedList', () => {
164164
expect(list.size()).toBe(1)
165165
})
166166

167+
it('Middle node of linked list', () => {
168+
const list = new LinkedList()
169+
list.addFirst(1)
170+
171+
// Middle node for list having single node
172+
expect(list.findMiddle().data).toEqual(1)
173+
174+
list.addLast(2)
175+
list.addLast(3)
176+
list.addLast(4)
177+
list.addLast(5)
178+
list.addLast(6)
179+
list.addLast(7)
180+
181+
// Middle node for list having odd number of nodes
182+
expect(list.findMiddle().data).toEqual(4)
183+
184+
list.addLast(10)
185+
186+
// Middle node for list having even number of nodes
187+
expect(list.findMiddle().data).toEqual(5)
188+
})
189+
167190
it('Check Iterator', () => {
168191
const list = new LinkedList()
169192

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.