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. Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies

    Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies. API 调用退出异常. (Except ...

  2. 清除Jquery动画的队列

    当我们在写页面效果时,有时希望当鼠标放到某个元素上,这时会有动态的效果,当鼠标移出时效果会消失.但实际中,如果快速的用鼠标指向元素并移出,反复几次.即便鼠标不再指向这个元素,但这个元素会不停的重复着动 ...

  3. poj1985&&第四次CCF软件认证第4题 求树的直径

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4216   Accepted: 2137 Case ...

  4. JDBC 学习笔记(七)—— CallableStatement

    在大型关系型数据库中,有一组为了完成特定功能的 SQL 语句集被称为存储过程(Stored Procedure),它是数据库中的对象. JDBC 使用 CallableStatement 对象,完成对 ...

  5. 【bzoj4800】[Ceoi2015]Ice Hockey World Championship 折半搜索

    题目描述 有n个物品,m块钱,给定每个物品的价格,求买物品的方案数. 输入 第一行两个数n,m代表物品数量及钱数 第二行n个数,代表每个物品的价格 n<=40,m<=10^18 输出 一行 ...

  6. C语言标准库 qsort bsearch 源码实现

    C语言是简洁的强大的,当然也有很多坑.C语言也是有点业界良心的,至少它实现了2个最最常用的算法:快速排序和二分查找. 我们知道,对于C语言标准库 qsort和 bsearch: a. 它是“泛型”的, ...

  7. ubuntu 安装自启动管理

    #sudo apt-get update #sudo apt-get install sysv-rc-conf 运行:#sudo sysv-rc-conf 也可以直接加入启动程序,例如把 /etc/i ...

  8. du 查看 資料夾 佔用空間

    查看 目前目錄使用的空間大小 du -h --max-depth=0 -h, --human-readable 查看 目前及下一屠的目錄 使用的空間大小 du -h --max-depth=1

  9. CentOS找不到想要的镜像版本?

    CentOS找不到想要的镜像版本? 情景: 当学习Linux时,一般教程不是最新的,教程里的CentOS版本也就不是最新的,这个时候, 在看着教程练习的时候就需要安装指定的版本,避免因为版本不同造成困 ...

  10. C#连接OleDBConnection数据库的操作

    对于不同的.net数据提供者,ADO.NET采用不同的Connection对象连接数据库.这些Connection对我们屏蔽了具体的实现细节,并提供了一种统一的实现方法. Connection类有四种 ...