Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 64149c4

Browse files
authored
adding comments 19. Remove Nth Node From End of List.js
1 parent 6da4bdd commit 64149c4

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
var removeNthFromEnd = function(head, n) {
2-
let count = 0
3-
let m = head
2+
let count = 0 // counter
3+
let m = head // store head node
4+
// count no. of nodes
45
while(head != null){
5-
head = head.next
6-
count++
6+
head = head.next// travel to next node
7+
count++// increment counter
78
}
9+
// create new ListNode
810
let k = new ListNode(0)
9-
let ans = k
11+
let ans = k// store head of k list
1012
for(let i = 0 ; i < count ; i++){
13+
// if i is not nth node from end we add that node to k list
1114
if(i != count - n){
1215
k = k.next = new ListNode(m.val)
1316
}
14-
m = m.next
17+
m = m.next// travel to next node
1518
}
1619
return ans.next
17-
};
20+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /