# LeetCode: Middle of the Linked List Solution

Several ways

## Approach: Calculate Length

Calculate list length

Iterate to the middle

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var middleNode = function (head) {2  let n = 03
4  let iter = head5  while (iter) {6    n++7    iter = iter.next8  }9
10  let mid = Math.floor(n / 2)11  iter = head12  while (mid--) {13    iter = iter.next14  }15
16  return iter17}```

## Approach: Fast and Slow

Slow pointer go 1 step, in the same time fast pointer go 2 steps

## Implementation

```1var middleNode = function (head) {2  for (var slow = (fast = head); fast && fast.next; ) {3    slow = slow.next4    fast = fast.next.next5  }6
7  return slow8}```

leetcode

two pointers

## Next Post

LeetCode: Permutation in String

Sep 18, 2021

Notice the constraints

Search Posts