From 1c9367d18f8398c16759d684da622f9e2998b517 Mon Sep 17 00:00:00 2001 From: Amit S Sahu Date: Mon, 21 Oct 2024 11:11:34 +0530 Subject: [PATCH] Time: 0 ms (100%), Space: 41.7 MB (46.44%) - LeetHub --- ...0019-remove-nth-node-from-end-of-list.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.java diff --git a/0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.java b/0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.java new file mode 100644 index 0000000..e667ccf --- /dev/null +++ b/0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.java @@ -0,0 +1,25 @@ +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + ListNode dummy = new ListNode(0); + dummy.next = head; + + ListNode temp = head; + int len = 0; + while (temp != null) { + len++; + temp = temp.next; + } + + int reqd = len - n; + temp = dummy; + + while (reqd > 0) { + temp = temp.next; + reqd--; + } + + temp.next = temp.next.next; + + return dummy.next; + } +}