Skip to content

Commit

Permalink
Time: 0 ms (100%), Space: 41.7 MB (46.44%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Oct 21, 2024
1 parent 661d2c7 commit 1c9367d
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 1c9367d

Please sign in to comment.