Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Simply further
Further simplify solution, this also matches the python solution shown in the video.
  • Loading branch information
Ahmad-A0 authored Jul 9, 2022
commit 756b78f0cb07e978babc459a6a70e37721b45ee4
41 changes: 18 additions & 23 deletions typescript/19-Remove-Nth-Node-From-End-of-List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};