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. struts2对properties资源的处理

    struts2对properties资源的处理 做了一些功能增强 包括: 可以读取项的描述 可以读取项所在的行号,文件路径等 实现方式 继承了java的java.util.Properties实现了一 ...

  2. Leetcode1--->数组中两数之和等于给定数

    题目: 给定一个数组nums,目标数target.在数组中找到两数之和为target的数,返回两数的下标举例: Given nums = [2, 7, 11, 15], target = 9, Bec ...

  3. 利用Solr的post工具对核心my_core执行删除操作

    <delete> <query>*:*</query> </delete> 将上面代码保存到delete_all.xml文件中,并使用Solr的post ...

  4. Python3 的异常处理

    Python3 的异常处理,在官方文档的 tutorial 中有说明. 这里把常用的异常处理方法都列出来,方便平时查找. 捕获异常基类 Python3 要求我们的异常必须继承 Exception 类. ...

  5. read(byte[] b)与readFully(byte[] b)

    转载于:http://yyzjava.iteye.com/blog/1178525 要搞清楚read(byte[] b)和readFully(byte[] b)的区别,可以从以下方面着手分析: 1.代 ...

  6. xampp下bugfree部署

    以Bugfree3.0.4为例,讲解如何搭建LAMP架构的Web服务器. Bugfree是一个XAMPP架构的网站,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的搭建XAMP ...

  7. 【Luogu】P3574FAR_FarmCraft(树形贪心)

    题解链接 想了一个错的贪心爆零了,气死. 题目链接 #include<cstdio> #include<cctype> #include<cstring> #inc ...

  8. 都系坤坤-微信助手V 0.1,解放双手发信息

    端午节刚过,相信大家在端午节都收到不少微信祝福信息,有复制长篇大论的祝福语群发的,有人工手打的简单祝福群发,我更喜欢人工手打带上称呼的祝福信息,这样看起来更加亲切. 那么问题来了,当你的通讯录里好友多 ...

  9. mybatis读取oracle中blob

    controller: byte[] blob = commonService.getPersonImage(bean.getIdCard()); String base64 = new String ...

  10. 【bzoj1717】[Usaco2006 Dec]Milk Patterns 产奶的模式 SA+二分

    Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个“模式”. John的牛奶按质 ...