Skip to content
Merged
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
32 changes: 32 additions & 0 deletions Data-Structures/Linked-List/MiddleOfLinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* A LinkedList based solution for Detect a Cycle in a list
Comment thread
10kartik marked this conversation as resolved.
Outdated
* https://afteracademy.com/blog/middle-of-the-linked-list
*/

function main () {
Comment thread
10kartik marked this conversation as resolved.
Outdated
/*
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.

Note:
* While Solving the problem in given link below, don't use main() function.
* Just use only the code inside main() function.
* The purpose of using main() function here is to avoid global variables.

Link for the Problem: https://leetcode.com/problems/middle-of-the-linked-list/
*/
const head = '' // Reference to head is given in the problem. So please ignore this line
Comment thread
10kartik marked this conversation as resolved.
Outdated
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
}

main()
Comment thread
10kartik marked this conversation as resolved.
Outdated