zoukankan      html  css  js  c++  java
  • Remove Nth Node From End of List

    问题:删除距离末尾n个距离的结点
    分析:先找出距离末尾n个距离的结点其距离开始的距离多少,然后再删除

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        int front;
        int dfs(ListNode *head,int n,int step)
        {
            if(head==NULL) return 0;
            int h=dfs(head->next,n,step+1)+1; 
            if(h==n) { front=step;}
            return h;
        }
        ListNode *removeNthFromEnd(ListNode *head, int n) {
            if(head==NULL) return head;
            front=0;
            dfs(head,n,1);
            ListNode *help = new ListNode(0);
            help->next=head;
            int t=1;
            ListNode *ret=help;
            while(1)
            {
                if(t==front) 
                {
                    ret->next=head->next;
                    delete head;
                    break;
                }
                ret=ret->next;
                head=head->next;
                t++;
            }
            return help->next;
        }
    };
    

      

  • 相关阅读:
    触发器
    累加求和存储过程
    check约束条件
    数据库的备份还原
    创建万能分页
    视图
    进销存存储过程
    函数存储过程
    数据库作业27~45
    数据库作业17~26
  • 原文地址:https://www.cnblogs.com/zsboy/p/3895326.html
Copyright © 2011-2022 走看看