Skip to content

Commit 849251a

Browse files
committed
update: simplify typescript/LC-19
1 parent b4ec8c9 commit 849251a

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

typescript/19-Remove-Nth-Node-From-End-of-List.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@
1111
*/
1212

1313
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
14-
let dummyList: ListNode = new ListNode(0)
15-
dummyList.next = head
16-
let count: number = 0
17-
let first: ListNode = dummyList
18-
let second: ListNode = dummyList
14+
let runner = head;
15+
let c = 0;
1916

20-
while (count < n) {
21-
second = second.next
22-
count++
17+
while (c != n && runner != null) {
18+
c++;
19+
runner = runner.next;
2320
}
24-
if (second === null) {
25-
dummyList.val = dummyList.next.val
26-
dummyList.next = dummyList.next.next
21+
22+
if (runner == null) {
23+
return head.next;
2724
}
28-
while (second.next !== null) {
29-
second = second.next
30-
first = first.next
25+
26+
let tail = head;
27+
28+
while (runner.next != null) {
29+
runner = runner.next;
30+
tail = tail.next;
3131
}
32-
first.next = first.next.next
3332

34-
return dummyList.next
33+
tail.next = tail.next.next;
34+
35+
return head;
3536
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy