【第22期】观点:IT 行业加班,到底有没有价值?

Remove Nth Node From End of List

原创 2015年07月08日 11:38:02
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        
        if(head == NULL)//头指针为空的情况
            return NULL;
            
        ListNode* h = head;
        while(h != NULL && n > 0)
        {
            n--;
            h = h->next;
        }
        
        if(h == NULL)//删除的是头指针的情况
            return head->next;
            
        ListNode* h1 = head;
        while(h->next != NULL)
        {
            h = h->next;
            h1 = h1->next;
        }
        
        h1->next = h1->next->next;
        
        return head;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

LeetCode --- 19. Remove Nth Node From End of List

题目链接:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list...

[leetcode]Remove Nth Node From End of List

新博文地址:<a id="cb_post_title_url" style="font-size: 1.5em; line-height: 1.5em; font-family: Verdana, Geneva, Arial, Helvetica, sans-se...

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

leetCode 刷题记录 Remove Nth Node From End of List

Question Given a linked list, remove the nth node from the end of list and return its head. For ...

[LeetCode] Remove Nth Node From End of List

Given a linked list, remove the <em style="margin

19 Remove Nth Node From End of List

19 Remove Nth Node From End of List链接:https://leetcode.com/problems/remove-nth-node-from-end-of-list...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)