Skip to content

Commit 14ce9cd

Browse files
committed
19
1 parent 4241871 commit 14ce9cd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

19.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @param {number} n
11+
* @return {ListNode}
12+
*/
13+
var removeNthFromEnd = function(head, n) {
14+
var temp = head,
15+
pos = head,
16+
count = 0;
17+
while(temp.next !== null){
18+
temp = temp.next;
19+
count++;
20+
if(count === n) break;
21+
}
22+
if(count !== n){
23+
head = head.next;
24+
return head;
25+
}
26+
while(temp.next !== null){
27+
temp = temp.next;
28+
pos = pos.next;
29+
}
30+
if(pos.next === null) return null;
31+
pos.next = pos.next.next;
32+
return head;
33+
};

0 commit comments

Comments
 (0)