您的位置:首页 > Web前端 > Node.js

Remove Nth Node From End of List

2017-12-06 15:51 323 查看
Remove Nth Node From End of List

the most important thing is that you must know the length of the  list;


l is length;



then remove is easy



And the complete code

class Solution {

public:

    ListNode* removeNthFromEnd(ListNode* head, int n) {

        int l = 0;

         ListNode* beg = new ListNode(0);     // you should create a new ListNode

         beg->next = head;

         ListNode* first = head;

        while(first)

        {

            l++;

            first = first->next;

              }

        

        l = l - n;

        first = beg;

        while(l>0)

        {  

            l--;

            first = first->next;

            }

        

        first->next = (first->next)->next;

        return beg->next;

    }

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: