leetcode 19:删除链表的倒数第N个节点

ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode*l1=head;
    int c=0;
    while(l1!=NULL)
    {
        c+=1;
        l1=l1->next;
    }
    if(c==n)return head->next;
    if(c<n)return NULL;
    ListNode*l2=head;
    for(int i=1;i<c-n;i++){
        l2=l2->next;
    }
    ListNode *l3=l2->next;
    l2->next=l3->next;
    return head;
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2020-10-18
  • 2021-11-29
  • 2021-11-05
  • 2021-09-17
  • 2018-07-07
猜你喜欢
  • 2019-11-24
  • 2021-10-24
  • 2021-06-03
  • 2021-06-20
  • 2021-10-31
  • 2021-08-02
相关资源
相似解决方案