-
Notifications
You must be signed in to change notification settings - Fork 44
/
Problem26.js
58 lines (54 loc) · 1.36 KB
/
Problem26.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Problem 26
//
// This problem was asked by Google.
//
// Given a singly linked list and an integer k, remove the kth last element from the list.
// k is guaranteed to be smaller than the length of the list.
//
// The list is very long, so making more than one pass is prohibitively expensive.
//
// Do this in constant space and in one pass.
//
// https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
//
// O(N) Time Complexity
// O(1) Space Complexity
// N is the length of the linked list
// eslint-disable-next-line
class LinkedListNode {
/**
* Initializes the start of a link list
* @param {*} val The value stored in linked list
*/
constructor(val) {
this.val = val;
this.next = null;
}
}
/**
* Removes the kth last element from this list
* @param {LinkedListNode} head
* @param {number} k
* @return {LinkedListNode}
*/
function findKthLast(head, k) {
let curr = head;
let runner = head; // k is guarenteed to be smaller, so we can just move runner
let count = 0;
// safety check
while (count < k && runner !== null) {
runner = runner.next;
count++;
}
let prev = null;
// move both curr and runner
while (runner !== null) {
runner = runner.next;
prev = curr;
curr = curr.next;
}
if (prev === null) return head.next;
prev.next = prev.next.next;
return head;
}
export default findKthLast;