Skip to content

Commit

Permalink
Time: 0 ms (100%), Space: 42.3 MB (5.72%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
avnisinngh committed Dec 31, 2024
1 parent 8cff94c commit a2018a2
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode fastp = head;
ListNode slowp = head;

for (int i = 0; i < n; i++)
fastp = fastp.next;

if (fastp == null)
return head.next;
while (fastp.next != null) {
fastp = fastp.next;
slowp = slowp.next;
}
ListNode delNode = slowp.next;
slowp.next = slowp.next.next;
delNode = null;
return head;

}
}

0 comments on commit a2018a2

Please sign in to comment.