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 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.

要求是一次过....看来基础还是有点差差的啊。

1.首先回顾一下链表的基本特性,从三点来分析下:

1> 存储分配方式,链表用一组任意的存储单元存放线性表的元素。

2>时间性能,查找O(n),插入和删除,找出某位置指针后,插入和删除时仅为O(1)。

3>空间性能,不需要分配存储空间,只要需要就可以malloc,所以也叫动态链表。

4> 插入和删除节点图示:

2.动态链表的创建(leedcode的链表是没有头结点),第一个形参参数是指针,要想改变指针的值,需要用引用或者指向指针的指针。

void CreateListHead(ListNode* &head, int n)
{
    int j=0;
    head = (ListNode*)malloc(sizeof(ListNode));
    head->next=NULL;
    head->val=v[j++];//v是用户输入的每个节点的val
    ListNode* p=head;
    for (int i=1;i<n;++i)
    {
        ListNode* newNode;
        newNode = (ListNode*)malloc(sizeof(ListNode));
        p->next=newNode;
        newNode->next=NULL;
        newNode->val=v[j++];
        p=p->next;
    }
}

3.所有调试代码

vector<int> v;
int num;
struct ListNode {
         int val;
         ListNode *next;
         ListNode(int x) : val(x), next(NULL) {}
     };
class Solution {
public:
    int getLength(ListNode *head){
        int length=0;
        while(head){
            ++length;
            head=head->next;
        }
        return length;
    }
    void deleteNode(ListNode* &head,int loc)//第一个位置是1
    {
        if(loc==1){
            head=head->next;
        }
        else
        {
            ListNode* p=head;
            int j=1;
            while (p->next&&j<loc-1)
            {
                p=p->next;
                ++j;
            }
            p->next=p->next->next;
        }
    }
    ListNode *removeNthFromEnd(ListNode *head, int n) {
        if(head==NULL)
            return NULL;
        ListNode* res=head;//需要新建局部变量
        int len=getLength(res);
        int loc=len-n+1;
        deleteNode(res,loc);
        return res;
    }
};

void CreateListHead(ListNode* &head, int n)
{
    int j=0;
    head = (ListNode*)malloc(sizeof(ListNode));
    head->next=NULL;
    head->val=v[j++];
    ListNode* p=head;
    for (int i=1;i<n;++i)
    {
        ListNode* newNode;
        newNode = (ListNode*)malloc(sizeof(ListNode));
        p->next=newNode;
        newNode->next=NULL;
        newNode->val=v[j++];
        p=p->next;
    }
}
int main()
{
    freopen("C:\\Users\\Administrator\\Desktop\\a.txt","r",stdin);
    cin>>num;
    for (int i=0;i<num;++i)
    {
        int temp;
        cin>>temp;
        v.push_back(temp);
    }
    ListNode* head=NULL;
    CreateListHead(head,3);
    Solution so;
    ListNode* res=so.removeNthFromEnd(head,1);
    return 0;
}
时间: 2024-10-11 09:00:46

Remove Nth Node From End of List(链表)的相关文章

leetCode 19. Remove Nth Node From End of List 链表

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 linked list: 1->2->3->4->5, and n = 2.    After removing the second node from the end, the linked lis

leetcode_19题——Remove Nth Node From End of List(链表)

Remove Nth Node From End of List Total Accepted: 54129 Total Submissions: 197759My Submissions Question Solution 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,

LeetCode: Remove Nth Node From End of List [019]

[题目] 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: G

【LeetCode】Remove Nth Node From End of List (2 solutions)

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 linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes

LeetCode 第 19 题 (Remove Nth Node From End of List)

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 linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode:Remove Nth Node From End of List

1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3.题目内容 英文:Given a linked list, remove the nth node from the end of list and return its head. 中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点 例如:

[leetcode]Remove Nth Node From End of List @ Python

原题地址:http://oj.leetcode.com/problems/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 linked list: 1->2->3->4->5, and n = 2. After removing the second n

LeetCode 019 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 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 bec

Remove Nth Node From End of List leetcode java

题目: 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: Gi

【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, 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