Skip to content

Commit

Permalink
Create remove nth node leetcode 19
Browse files Browse the repository at this point in the history
  • Loading branch information
101Bhavya authored Oct 20, 2022
1 parent d74a84d commit 02e45c5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions RemoveNthNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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 RemoveNthNode {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode fast=head;
ListNode slow=head;
for(int i=0;i<n;i++){
fast=fast.next;
}
if(fast==null){
return head.next;
}
while(fast.next!=null){
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return head;
}
}

0 comments on commit 02e45c5

Please sign in to comment.