Simple c solution


  • -1
    B

    case1:
    if head node is deleted.
    case2:
    first move the slow pointer to n positions.Then move two pointers.stop the iteration where slow->next==NULL.Delete the position which we got now..
    /**

    • Definition for singly-linked list.
    • struct ListNode {
    • int val;
      
    • struct ListNode *next;
      
    • };
      /
      struct ListNode
      removeNthFromEnd(struct ListNode* head, int n) {
      int s=0,i=1;
      struct ListNode* slow=head,*fast=head,*prev=NULL;
      while(i<n){
      slow=slow->next;
      i++;
      }
      if(slow->next==NULL){
      head=head->next;
      free(fast);
      }
      else{
      while(slow->next){
      prev=fast;
      fast=fast->next;
      slow=slow->next;
      }
      prev->next=fast->next;
      free(fast);
      }
      return head;

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.