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

原创 2016年05月30日 21:39:10

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

For example,

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.

#include <iostream>
#include <vector>
using namespace std;


struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        //如果为空链表
        if (NULL == head)
        {
            return head;
        }
        //如果链表只有一个节点,且n为1
        if (NULL == head->next && 1 == n)
        {
            delete head;
            return NULL;
        }
        ListNode * ptr = head;
        --n;
        while (n>0 && ptr)
        {   
            ptr = ptr->next;
            --n;
        }
        //ptr不为NULL,则存在第n个节点,且此时ptr指向第n个节点
        if (ptr != NULL)
        {
            //这种情况表示待删除的节点为头节点
            if (ptr->next == NULL)
            {
                ListNode * tmp = head;
                head = head->next;
                delete tmp;
            }
            else
            {
                ListNode * pre = NULL;
                ListNode * curr = head;
                //curr指针所指的节点到ptr指针所指的节点相距n个节点,将ptr移到指向最后一个节点时,此时curr节点指向倒数第n个节点
                while(NULL != ptr->next)
                {
                    ptr = ptr->next;
                    pre = curr;
                    curr = curr->next;
                }
                //此时curr指向倒数第n个节点,pre指向待删除节点的前一个节点
                pre->next = curr->next;
                delete curr;
            }
        }       
        return head;
    }
};
int main()
{
    ListNode * head = (ListNode*)malloc(sizeof(ListNode));
    head->val = 1;
    ListNode * p = (ListNode*)malloc(sizeof(ListNode));
    p->val = 2;
    p->next = NULL;
    head->next = p;
    Solution s;
    head = s.removeNthFromEnd(head,1);
    ListNode *ptr = head;
    while(ptr != NULL)
    {
        cout<<ptr->val<<'\t';
        ptr = ptr->next;
    }
    cout<<endl;
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

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 ex...
  • buptlrw
  • buptlrw
  • 2016年02月19日 18:47
  • 333

LeetCode 第十九题 Remove Nth Node From End of List

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

和大神们学习每天一题(leetcode)-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中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...

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 ex...
  • LZTree
  • LZTree
  • 2017年03月26日 21:28
  • 94

【LeetCode-面试算法经典-Java实现】【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】

【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a l...

《leetCode》: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, ...

[C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)

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

Leetcode_19_Remove Nth Node From End of List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41778305 Given a linked list, ...

LeetCode Remove Nth Node From End of List 删除倒数第n个元素

这里的Note的意思应该就是可以不做特殊处理n值了。不过其实特殊处理也就一句语句。 思路: 快指针和慢指针的概念,先一个指针先走,到了离起点n个节点远的时候,慢指针走,等到快指针走到结尾,那么慢指...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode第19题之 Remove Nth Node From End of List
举报原因:
原因补充:

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