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. C语言遇到的小问题

    堆栈区内存大小,2M或者4M,如果char够用,你就按照char去开,就只有int的1/4啦 printf("%s",s[i])不能运行 case 's': s = va_arg( ...

  2. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it

    链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...

  3. pip install ImportError: cannot import name main

    在Ubuntu上用pip install装ansible时报错 root@user:~# pip install --no-cache-dir ansible -i http://mirrors.al ...

  4. 使用 SOAPUI 测试Web Service

    原文地址:https://www.ibm.com/developerworks/cn/webservices/1106_webservicessecurity/index.html(里面内容比较多不用 ...

  5. [SCOI2016] 背单词 (Trie 树,贪心)

    题目链接 大致题意 给你 \(n\) 个字符串, 要求你给出最小的代价. 对于每个字符串: 1.如果它的后缀在它之后,那么代价为 \(n^2\). 2.如果一个字符串没有后缀,那么代价为 \(x\), ...

  6. [BZOJ3261] 最大异或和 (异或前缀和,可持久化Trie)

    Description 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.Ax:添加操作,表示在序列末尾添加一个数x,序列的长度N+1. 2.Q l r x:询问操作, ...

  7. codeforces 900D 数论+组合+容斥原理

    问有多少个这样的数字序列 所有数的GCD等于x 并且 所有数的和等于y 题解: 非常难有思路啊 看题解后过的. 考虑序列GCD为x的倍数 即GCD = n*x 和当然都为y 这个条件不要忘了 这样我们 ...

  8. luogu 1142 轰炸 最多共线点数

    题目链接 题意 给定\(n(n\leq 700)\)个点,问共线的点最多有多少个? 思路 \(O(n^3)\):枚举两个顶点确定一条直线,再看有多少个顶点在这条直线上.讲道理会T. \(O(n^2lo ...

  9. Linux内存管理【转】

    转自:http://www.cnblogs.com/wuchanming/p/4360264.html 转载:http://www.kerneltravel.net/journal/v/mem.htm ...

  10. poj 1459(网络流)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 26688   Accepted: 13874 D ...