Skip to content

Commit 7f1387f

Browse files
fix(2.2): redo returnKthToLast (careercup#67)
1 parent 96b534f commit 7f1387f

File tree

2 files changed

+45
-73
lines changed

2 files changed

+45
-73
lines changed
Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
1-
var linkedList = function(value) {
2-
this.value = value;
3-
this.next = null;
4-
};
1+
const LinkedList = require("../util/LinkedListX");
2+
3+
var findKthToLast = function(k, list) {
4+
// do iteratively
5+
//define two pointers , fast and slow pointer
6+
let fast = list.head
7+
let slow = list.head
8+
9+
//Move fast pointer k steps in the linkedlist while slow remains at head
10+
for(let i=0;i<k;i++){
11+
if(fast === null) return null //k is larger than length of linked list
12+
fast = fast.next
13+
}
14+
// move both pointers at the same time, slow pointer will exit at kth node from the end
15+
while(fast !== null){
16+
fast =fast.next
17+
slow=slow.next
518

6-
var findKthToLast = function(k, head) {
7-
// do recursively
8-
if (head === null || k < 1) {
9-
return;
10-
} else if (k === 1) {
11-
console.log(head.value);
12-
findKthToLast(k, head.next);
13-
} else {
14-
findKthToLast(k-1, head.next);
1519
}
16-
};
20+
return slow
1721

22+
};
1823
/* TESTS */
19-
var a = new linkedList('1');
20-
var b = new linkedList('2');
21-
var c = new linkedList('3');
22-
var d = new linkedList('4');
23-
var e = new linkedList('5');
24-
var f = new linkedList('6');
25-
var g = new linkedList('7');
24+
let l = new LinkedList();
25+
l.append(1)
26+
l.append(2)
27+
l.append(3)
28+
l.append(4)
29+
l.append(5)
30+
2631

27-
a.next = b;
28-
b.next = c;
29-
c.next = d;
30-
d.next = e;
31-
e.next = f;
32-
f.next = g;
3332

34-
findKthToLast(3, a);
33+
console.log(findKthToLast(3, l));
3534

36-
findKthToLast(10, a);
35+
console.log(findKthToLast(10, l));
3736

38-
findKthToLast(-1, a);
37+
console.log(findKthToLast(-1, l));
3938

40-
findKthToLast(0, a);
39+
console.log(findKthToLast(0, l));
4140

42-
findKthToLast(1, a);
41+
console.log(findKthToLast(1, l));
Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,21 @@
1-
var LinkedList = function(value) {
2-
this.value = value;
3-
this.next = null;
4-
};
1+
const LinkedList = require("../util/LinkedListX");
2+
const printList = require("../util/printList");
3+
4+
let deleteMidNode = function(node) {
55

6-
var deleteMidNode = function(midNode) {
7-
var node = midNode;
8-
while (node !== null && node.next !== null) {
6+
if(node !== null && node.next !== null) {
97
node.value = node.next.value;
10-
if (node.next.next === null) {
11-
node.next = null;
12-
}
13-
node = node.next;
14-
}
8+
node.next = node.next.next;
9+
}
1510
};
1611

17-
// a -> b -> c -> d -> e -> f, input c
18-
// a -> b -> *d -> d -> e -> f
19-
// a -> b -> d -> *e -> e -> f
20-
// a -> b -> d -> e -> *f -> f
21-
// a -> b -> d -> e -> f -> *null
22-
2312
/* TEST */
2413

25-
var printList = function(head) {
26-
while(head !== null) {
27-
console.log(head.value);
28-
head = head.next;
29-
}
30-
console.log('done printing');
31-
};
32-
33-
var a = new LinkedList('a');
34-
var b = new LinkedList('b');
35-
var c = new LinkedList('c');
36-
var d = new LinkedList('d');
37-
var e = new LinkedList('e');
38-
var f = new LinkedList('f');
39-
40-
a.next = b;
41-
b.next = c;
42-
c.next = d;
43-
d.next = e;
44-
e.next = f;
14+
let list = new LinkedList();
15+
for (let item of [1, 2, 3, 4, 5, 6]) {
16+
list.append(item);
17+
}
4518

46-
printList(a);
47-
deleteMidNode(c);
48-
printList(a);
19+
printList(list.head);
20+
deleteMidNode(list.head.next);
21+
printList(list.head);

0 commit comments

Comments
 (0)