Remove Nth Node From End of List
Approach1: Two Pass
/**
* 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 dummy = new ListNode(-1);
dummy.next = head;
ListNode current = dummy;
int length = 0;
while(current != null){
length++;
current = current.next;
}
length -= n;
current = dummy;
while(--length > 0){
current = current.next;
}
current.next = current.next.next;
return dummy.next;
}
}Approach2: One Pass (optimal)
Java Version (optimal)
Last updated