Skip to content

Commit

Permalink
Simply further
Browse files Browse the repository at this point in the history
Further simplify solution, this also matches the python solution shown in the video.
  • Loading branch information
Ahmad-A0 authored Jul 9, 2022
1 parent 849251a commit 756b78f
Showing 1 changed file with 18 additions and 23 deletions.
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
};

0 comments on commit 756b78f

Please sign in to comment.