From 849251a9ab1c21e36f27c7d5f817e1ca5c23b675 Mon Sep 17 00:00:00 2001 From: Sandeep Gamot Date: Sat, 9 Jul 2022 12:27:29 +0530 Subject: [PATCH 1/2] update: simplify typescript/LC-19 --- .../19-Remove-Nth-Node-From-End-of-List.ts | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/typescript/19-Remove-Nth-Node-From-End-of-List.ts b/typescript/19-Remove-Nth-Node-From-End-of-List.ts index c9e2b6400..c73e7ecbe 100644 --- a/typescript/19-Remove-Nth-Node-From-End-of-List.ts +++ b/typescript/19-Remove-Nth-Node-From-End-of-List.ts @@ -11,25 +11,26 @@ */ function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { - let dummyList: ListNode = new ListNode(0) - dummyList.next = head - let count: number = 0 - let first: ListNode = dummyList - let second: ListNode = dummyList + let runner = head; + let c = 0; - while (count < n) { - second = second.next - count++ + while (c != n && runner != null) { + c++; + runner = runner.next; } - if (second === null) { - dummyList.val = dummyList.next.val - dummyList.next = dummyList.next.next + + if (runner == null) { + return head.next; } - while (second.next !== null) { - second = second.next - first = first.next + + let tail = head; + + while (runner.next != null) { + runner = runner.next; + tail = tail.next; } - first.next = first.next.next - return dummyList.next + tail.next = tail.next.next; + + return head; } From 756b78f0cb07e978babc459a6a70e37721b45ee4 Mon Sep 17 00:00:00 2001 From: aa0 <71089234+Ahmad-A0@users.noreply.github.com> Date: Sat, 9 Jul 2022 11:13:59 +0100 Subject: [PATCH 2/2] Simply further Further simplify solution, this also matches the python solution shown in the video. --- .../19-Remove-Nth-Node-From-End-of-List.ts | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/typescript/19-Remove-Nth-Node-From-End-of-List.ts b/typescript/19-Remove-Nth-Node-From-End-of-List.ts index c73e7ecbe..24268c9fb 100644 --- a/typescript/19-Remove-Nth-Node-From-End-of-List.ts +++ b/typescript/19-Remove-Nth-Node-From-End-of-List.ts @@ -11,26 +11,21 @@ */ function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { - let runner = head; - let c = 0; - - while (c != n && runner != null) { - c++; - runner = runner.next; - } - - if (runner == null) { - return head.next; - } - - let tail = head; - - while (runner.next != null) { - runner = runner.next; - tail = tail.next; - } - - tail.next = tail.next.next; - - return head; -} + let dummy: ListNode = new ListNode(0, head) + let left = dummy + let right = head + + while (n > 0) { + right = right.next + n -= 1 + } + + while (right) { + left = left.next + right = right.next + } + + // delete + left.next = left.next.next + return dummy.next +}; 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