problem:

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.

删除单链表的倒数第n个节点

thinking:

(1)这里的 head 是头指针。指向第一个结点!!

!别搞混了。

(2)为了避免反复计数,採用双指针,先让第一个指针走n-1步,再一起走,这样,等前面指针走到最后一个非空结点时。后面一个指针正好指向待删除结点的前驱!!!

(3)延伸:

头结点不是必须的,一般不用。经常使用的是用一个头指针head指向第一个元素结点!!

!。!这道题就是!!!!

/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (head == NULL)
return NULL; ListNode *pPre = NULL;
ListNode *p = head;
ListNode *q = head;
for(int i = 0; i < n - 1; i++)
q = q->next; while(q->next)
{
pPre = p;
p = p->next;
q = q->next;
} if (pPre == NULL)
{
head = p->next;
delete p;
}
else
{
pPre->next = pPre->next->next;
delete p;
} return head;
}
};

leetcode 题解 || Remove Nth Node From End of List 问题的更多相关文章

  1. [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, Give ...

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

  3. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

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

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

  5. 【leetcode】Remove Nth Node From End of List(easy)

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

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

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

  7. 【JAVA、C++】LeetCode 019 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, Give ...

  8. Java [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 ...

  9. 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, Give ...

随机推荐

  1. Selenium WebDriver- 指定页面加载时间

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

  2. RIP 路由协议

    RIP动态路由选择协议 routing information protocol     IGP   小范围   路由器限制为15台  超过可能无法收敛   收敛概念  在一个域内  各个路由器知道各 ...

  3. [python][oldboy][dict] 遍历字典

    # coding=utf8 dict_info = {"abc": 1, 1: "liuzhipeng"} for k, v in dict_info.item ...

  4. 代码托管在阿里云并用git拉取

    1.在阿里云上注册一个账号,https://code.aliyun.com/ 2.创建组 3.添加组员权限 4.添加项目 5.复制项目地址 安装git   https://git-scm.com/   ...

  5. Linux Shell系列教程之(九)Shell判断 if else 用法

    本文是Linux Shell系列教程的第(九)篇,更多shell教程请看:Linux Shell系列教程 判断语句是每个语言都必不可少的关键语法,Shell命令当然也不例外.今天就给大家介绍下Shel ...

  6. 【Luogu】P3232游走(高斯消元解概率)

    题目链接 参见远航之曲dalao的题解,我再写一遍的话就没啥意思了. #include<cstdio> #include<cstring> #include<algori ...

  7. css3制作扇形菜单

    工作中网页中有一个扇形的导航菜单,以前没有接触过,参考了http://www.w3cplus.com/css3/building-a-circular-navigation-with-css-tran ...

  8. 刷题总结——稻草人(bzoj4237cdq分治)

    题目: Description JOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,JOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样, ...

  9. zoj 3791 An Easy Game dp

    An Easy Game Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Edward and Flandre play a ga ...

  10. hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

    Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...