题目

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.

代码:9ms过集合

/**
* 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) {
if ( !head ) return head;
ListNode dummy(-);
dummy.next = head;
ListNode *p1 = &dummy, *p2 = &dummy;
for (size_t i = ; i < n; ++i, p2=p2->next);
for (; p2->next; p1=p1->next, p2=p2->next);
ListNode *tmp = p1->next;
p1->next = p1->next==NULL ? NULL : p1->next->next;
delete tmp;
return dummy.next;
}
};

Tips:

双指针技巧:

  a. p2先走n步

  b. p1和p2一起走,直到p2走到最后一个元素

  c. 删除元素

注意再删除元素的时候,保护一下p1->next指针不为NULL。

==========================================

第二次过这道题,思路比较清晰。由于受到Rotate List这道题的影响,第一次把p1 = head 和 p2 = head了;之后改成了p1 = &dummpy和p2 = &dummpy就AC了。

/**
* 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) {
if ( !head ) return head;
ListNode dummpy(-);
dummpy.next = head;
ListNode* p1 = &dummpy;
ListNode* p2 = &dummpy;
for ( int i=; i<n; ++i ) p2 = p2->next;
while ( p2 && p2->next)
{
p1 = p1->next;
p2 = p2->next;
}
p1->next = p1->next ? p1->next->next : NULL;
return dummpy.next;
}
};

【Remove Nth Node From End of List】cpp的更多相关文章

  1. leetcode 【 Remove Nth Node From End of List 】 python 实现

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

  2. 【LeetCode】19. 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 r ...

  3. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  4. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

  5. Merge Two Sorted Lists & Remove Nth Node From End of List

    1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...

  6. leetcode-algorithms-19 Remove Nth Node From End of List

    leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...

  7. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

  8. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  9. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

随机推荐

  1. swagger + springboot

    参考文章:  https://blog.csdn.net/xupeng874395012/article/details/68946676/ https://blog.csdn.net/hry2015 ...

  2. CentOS服务器初始化设置

    CentOS服务器初始化设置 以阿里云服务器为例 一.挂载硬盘 1.磁盘分区 fdisk -l #查看设备,一般可以看到设备名为/dev/xvdb,或者为/dev/vdb(阿里云io优化型) fdis ...

  3. OpenSSL context 的几个参数

    NAME SYNOPSIS DESCRIPTION NOTES BUGS RETURN VALUES EXAMPLES SEE ALSO NAME SSL_CTX_set_verify, SSL_se ...

  4. LeetCode Add Digits (规律题)

    题意: 将一个整数num变成它的所有十进制位的和,重复操作,直到num的位数为1,返回num. 思路: 注意到答案的范围是在区间[0,9]的自然数,而仅当num=0才可能答案为0. 规律在于随着所给自 ...

  5. wamp端口冲突

    因为端口冲突,Apache服务不能运行. 解决方法: 点击wamp图标 => Apache => use a port other than 80 => 输入新的端口,即可. 然后 ...

  6. IOS 核心动画(Core Animation)

    Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它 能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就 可以实现非常强大的功能. Core ...

  7. hdu1150&&POJ1325 Machine Schedule---最小点覆盖

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1150 题目大意: 给你两台机器A和B,A机器有n种模式,B机器有m种模式,初始时都是0,现在给你k个 ...

  8. 机器学习_线性回归和逻辑回归_案例实战:Python实现逻辑回归与梯度下降策略_项目实战:使用逻辑回归判断信用卡欺诈检测

    线性回归: 注:为偏置项,这一项的x的值假设为[1,1,1,1,1....] 注:为使似然函数越大,则需要最小二乘法函数越小越好 线性回归中为什么选用平方和作为误差函数?假设模型结果与测量值 误差满足 ...

  9. python_输出100:200内的素数

    sushu=[] for i in range(100,201): for j in range(2,i): if i%j==0: break if i==j+1: sushu.append(i) p ...

  10. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...