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 第十九题 Remove Nth Node From End of List

一,问题描述 1,删除一个单链表末尾开始算起的第n个结点,然后返回该单链表。 2,例如: 输入: 1->2->3->4->5 其中n=2; 输出: 1->2->3->5; 3,解题思...

Remove Nth node from End of list--LeetCode

题目: Given a linked list, remove the nth node from the end of list and return its head. For examp...

【LeetCode】Remove Nth Node From End of List

题目介绍: 下面给出两种算法: 算法一

Remove Nth Node From End of List

题目名称 Remove Nth Node From End of List —LetCode链接描述 Given a linked list, remove the nth node from t...

[leetcode: Python]19. Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.For example,Given ...
  • NXHYD
  • NXHYD
  • 2017-05-19 08:28
  • 128

LeetCode#19. Remove Nth Node From End of List

题目:删除链表倒数第n个元素

Remove Nth Node From End of List

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

Remove Nth Node From End of List

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

LeetCode-19 Remove Nth Node From End of List(移除尾部第N个节点)

LeetCode-19 Remove Nth Node From End of List Given a linked list, remove the nth node from the end o...

LeetCode第19题之 Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.For example,Given ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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