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.

题意转换一下就是求解单链表的倒数第k个节点,解法是设置两个指针,一个在前一个在后,之间的距离为k,同时向前移动,有点类似滑动窗口,当第一个指针到达链表尾的时候,第二个指针即为倒数第k个节点,具体的见编程之美

ListNode *removeNthFromEnd(ListNode *head, int n){
if(head == NULL) return NULL;
ListNode *q = head,*p = head;
for(int i = ; i < n - ; ++ i)
q = q -> next;
ListNode *pPre = NULL;
while(q->next){
pPre = p;
p = p->next;
q = q->next;
}
if(pPre == NULL){
pPre = p->next;
delete p;
}else{
pPre ->next = p ->next;
delete p;
}
return head;
}

Leetcode Remove Nth Node From End of List的更多相关文章

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

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

  2. [LeetCode] 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 ...

  3. [leetcode]Remove Nth Node From End of List @ Python

    原题地址:http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意: Given a linked list, remo ...

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

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

  6. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  7. leetcode remove Nth Node from End python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  8. LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

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

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

随机推荐

  1. Jsonp跨域访问原理和实例

    >>什么是跨域 出于安全方面的考虑,页面中的JavaScript无法访问其他服务器上的数据,当前域名的js只能读取同域下的窗口属性,即同源策略.而跨域就是通过某些手段来绕过同源策略限制,实 ...

  2. .NET MVC4 数据验证Model(二)

      一.概述 MVC分为ViewModel.Control.View,对数据的封装MVC做的很好,确实是不错的WEB框架,针对MVC的ViewModel封装的也是相当的不错,最近做一个MVC的项目,采 ...

  3. 【PHP Cookie&&Session】

    大部分的人都知道Cookie,但是可能不了解Session,现在对这两者进行解释. 问题的提出: 有些网站会提示用户在一定的时间之内免登陆,这是用的什么技术?答案是Cookie技术. 有些购物网站会提 ...

  4. c++ 左值右值 函数模板

    1.先看一段代码,这就是一种函数模板的用法,但是红色的部分如果把a写成a++或者写成一个常量比如1,都是编译不过的,因为如果是a++的话,实际上首先是取得a的 值0,而0作为一个常量没有地址.写成1也 ...

  5. 攻城狮在路上(叁)Linux(十一)--- 用户与用户组、文件权限、目录配置

    一.用户与用户组: 3个概念:文件所有者(user).用户组(group).其他人(others). /etc/passwd  <==存放所有的用户名 /etc/shadow  <==存放 ...

  6. COALESCE NVL NVL2 DECODE

    1 COALESCE 語法:COALESCE(expr1, expr2, ..., exprn) n>=2 作用:COALESCE returns the first non-null expr ...

  7. php计算几分钟前、几小时前等

    function format_date($time){ $t=time()-$time; $f=array( '=>'年', '=>'个月', '=>'星期', '=>'天' ...

  8. shell判断文件是否存在

    转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...

  9. vs2015 MFC工程添加消息响应函数

    真不知道这PPT怎么描述的..最后窝找到了解决方法如上图.. 下次找MSDN解决问题好了..而且我们并不知道他所说的这个IDE到底是哪个厂商哪个版本的IDE这就很困惑 不过呢..它主要是让我们添加消息 ...

  10. LoadRunner 场景运行error的几种情况

    一. Error -27727: Step download timeout (120 seconds)has expired when downloading resource(s). Set th ...