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; + } +}