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

/**
* 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) {
ListNode* pFirst = head, *pSecond = head;
for(int i = ; i < n; i++){
pFirst = pFirst->next;
}
if(pFirst == NULL){
return head->next;
}
while(pFirst->next){
pFirst = pFirst->next;
pSecond = pSecond->next;
}
pSecond->next = pSecond->next->next;
return head;
}
};

19.Remove Nth Node From End of List(List; Two-Pointers)的更多相关文章

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

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

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

  3. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

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

  5. [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 ...

  6. 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, ...

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

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

  10. 【一天一道LeetCode】#19. Remove Nth Node From End of List

    一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...

随机推荐

  1. L192 Virgin Galactic Completes Test of Spaceship to Carry Tourists

    Virgin Galactic says its spacecraft designed to launch tourists into space completed an important te ...

  2. nginx配置文件的性能优化

    1.nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数.worker_processes 8; 2.为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或 ...

  3. 20155223 2016-2017-2 《Java程序设计》第8周学习总结

    20155223 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第14章 Channel是Java NIO用来衔接数据节点的功能,可定义缓冲区容量.标记内容 ...

  4. redis的maxmemory与maxmemory-policy关系

    如果redis配置了maxmemory和maxmemory-policy策略,则当redis内存数据达到maxmemory时,会根据maxmemory-policy配置来淘汰内存数据,以避免OOM.r ...

  5. Spring boot的@Configuration

    就在我惊艳于spring 4的AbstractAnnotationConfigDispatcherServletInitializer小巧简洁(如下)的时候却发现spring boot下面竟然无效. ...

  6. 推荐一个React 入门的教程

    推荐一个React 入门的教程 react 入门实例教程 Github地址:https://github.com/ruanyf/react-demos

  7. 解决:win8.1 oepnvpn客户端 redirect-gateway def1无效,自动获取的IP没有网关问题

    解决:win8.1 oepnvpn客户端 redirect-gateway def1无效,自动获取的IP没有网关问题 该问题是操作系统权限问题,需要将程序设置为以管理员模式运行和以windows7兼容 ...

  8. column count of mysql.proc is wrong. expected 20,found 16. the table is probably corruptd.

    1558 1547 column count of mysql.proc is wrong. expected 20,found 16. the table is probably corruptd. ...

  9. bzoj 3879: SvT

    Description (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始 ...

  10. 峰Spring4学习(7)spring对JDBC的支持

    第一节: 工程结构: 1)student.java: package com.cy.model; public class Student { private int id; private Stri ...