Skip to content

Commit 877408e

Browse files
authored
Merge pull request neetcode-gh#427 from SandeepGamot/ts-solutions
update: simplify typescript/LC-19
2 parents b4ec8c9 + 756b78f commit 877408e

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,21 @@
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
19-
20-
while (count < n) {
21-
second = second.next
22-
count++
23-
}
24-
if (second === null) {
25-
dummyList.val = dummyList.next.val
26-
dummyList.next = dummyList.next.next
27-
}
28-
while (second.next !== null) {
29-
second = second.next
30-
first = first.next
31-
}
32-
first.next = first.next.next
33-
34-
return dummyList.next
35-
}
14+
let dummy: ListNode = new ListNode(0, head)
15+
let left = dummy
16+
let right = head
17+
18+
while (n > 0) {
19+
right = right.next
20+
n -= 1
21+
}
22+
23+
while (right) {
24+
left = left.next
25+
right = right.next
26+
}
27+
28+
// delete
29+
left.next = left.next.next
30+
return dummy.next
31+
};

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