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

    @Genymotion相关文件下载地址 http://pan.baidu.com/s/1nu9nReh @虚拟机网络代理设置 Normal 0 7.8 磅 0 2 false false false ...

  2. Delphi基本数据类型---枚举、子界、集合、数组

    参考:http://blog.csdn.net/qustdong/article/details/9230743 参考:http://www.cnblogs.com/xumenger/p/440222 ...

  3. Pyqt phonon的使用

    本文是用Pyqt实现了下网上一个Qt版大牛关于phonon的介绍 Qt phonon地址:http://wenku.baidu.com/link?url=nH_dZ8lZbXHy8N5__8jAWLX ...

  4. Tiny Rss Reader - 迷你RSS阅读器

    发布新软件 TinyRss: Windows平台上的一个小巧的Rss阅读器. 用户界面: 项目地址: https://github.com/movsb/tinyrss.git 测试下载: http:/ ...

  5. Tkprof工具详解一

      注明:一些文章是从别人的博客中转载过来的,方便自己以后查阅:在数据库生成的oracle trace文件中,可读性是比较差的,此时可使用tkprof工具来格式化trace文件,tkprof是一个命令 ...

  6. Codeforces Round #363 LRU(概率 状压DP)

    状压DP: 先不考虑数量k, dp[i]表示状态为i的概率,状态转移方程为dp[i | (1 << j)] += dp[i],最后考虑k, 状态表示中1的数量为k的表示可行解. #incl ...

  7. .NET NLog 详解 (三) - LayoutRender

    这期将NLog Git版本指向2005-06-09,NLog v0.9 released.这个时候的代码结构升级为这样: 和上期的版本相比,最明显的莫过于原先的Appender全套更名为Target. ...

  8. 如何设计一个优秀的API(转)

    到目前为止,已经负责API接近两年了,这两年中发现现有的API存在的问题越来越多,但很多API一旦发布后就不再能修改了,即时升级和维护是必须的.一旦API发生变化,就可能对相关的调用者带来巨大的代价, ...

  9. 【java基础】选择排序and冒泡排序

    前言 : 今天学习的是J2SE视频里的第五章,数组部分,它里面留了一个经典的作业,就是让我们去从1倒9按一定规格排序,这让我想起了学习vb的时候最最让我头疼的两种排序方法,选择排序法 和 冒泡排序法. ...

  10. 战斗住的DPS才是DPS,持续的执行力才是执行力

    工作久了,真的发现执行力这个东西太难被贯彻,计划时信心满满,冲劲十足,持续一段时间后就喇叭腔了.