Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

algorithm: find the middle of linked-list #1096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
raklaptudirm merged 5 commits into TheAlgorithms:master from 10kartik:develop
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
27 changes: 27 additions & 0 deletions Data-Structures/Linked-List/MiddleOfLinkedList.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* A LinkedList based solution for finding middle node of linked list.
* https://afteracademy.com/blog/middle-of-the-linked-list
*/
class MiddleOfLL {
solution (head) {
/*
Problem Statement:
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.

Link for the Problem: https://leetcode.com/problems/middle-of-the-linked-list/
*/
let fast = head
let slow = head

if (head.next == null) { return head }

while (fast != null && fast.next != null) {
fast = fast.next.next
slow = slow.next
}
return slow
}
}

export { MiddleOfLL }
41 changes: 41 additions & 0 deletions Data-Structures/Linked-List/test/MiddleOfLinkedList.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { MiddleOfLL } from '../MiddleOfLinkedList'
import { LinkedList } from '../SinglyLinkedList'

describe('MiddleOfLinkedList', () => {
it('middle node Of linked list - even length ', () => {
const list = new LinkedList()
list.addFirst(1)
list.addLast(2)
list.addLast(3)
list.addLast(4)
list.addLast(5)
list.addLast(6)
list.addLast(7)

const MiddleNodeOfLinkedList = new MiddleOfLL().solution(list.headNode)
expect(MiddleNodeOfLinkedList.data).toEqual(4)
})

it('middle node of linked list - odd length ', () => {
const list = new LinkedList()
list.addFirst(10)
list.addLast(20)
list.addLast(30)
list.addLast(40)
list.addLast(50)
list.addLast(60)
list.addLast(70)
list.addLast(80)

const MiddleNodeOfLinkedList = new MiddleOfLL().solution(list.headNode)
expect(MiddleNodeOfLinkedList.data).toEqual(50)
})

it('middle node of linked list - length 1 ', () => {
const list = new LinkedList()
list.addFirst(100)

const MiddleNodeOfLinkedList = new MiddleOfLL().solution(list.headNode)
expect(MiddleNodeOfLinkedList.data).toEqual(100)
})
})

AltStyle によって変換されたページ (->オリジナル) /