shlogg · Early preview
Prashant Mishra @prashantrmishra

Remove Nth Node From End Of Linked List In Constant Space

Remove Nth Node from End of Linked List: two approaches to solve this problem with time complexity O(N) and space complexity O(1).

Problem

//tc:O(N) n is the length of the linked list
//sc :(1) constant
/**
 * 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) {
        // base case
        if(head.next ==null && n ==1) return null;
        ListNode node1 = head;
        ListNode node2 = head;
        //prev is required for edge case example if the...