-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e931a4c
Added Middle of linked-list implementation.
b0ddd37
Added Middle of LL function and tests
a70a317
Refactor: Added method in singly LL and its tests
fbce118
Refactor: Method name and inline test calls
fdeca6a
Use `!== null` instead of `!= null`
appgurueu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
Data-Structures/Linked-List/MiddleOfLinkedList.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
10kartik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.