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个元素,并返回修改后的链表。

要求: 只经过一次遍历完成上述操作。

经典面试题,找到一个链表的倒数第N个元素的衍伸。 在本题中需要额外的记录第N个元素的上一个元素,用于元素删除。

寻找链表的倒数第N个元素,设置两个指针,分别从链表的头部出发,一个先遍历N个元素, 然后两个指针同时向后遍历,当前一个指针到达链表的尾部时,后一个指针则到达第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 *pre,*first,*last;
ListNode ans();
pre=first=last=head;
ans.next=pre;
for(int i=;i<n;i++)
first=first->next; //find faster pointer while(first!=NULL)
{
first=first->next;
pre = last;
last=last->next;
}
pre->next = last->next;
if(last==head) return head->next;
else return ans.next;
}
};

转载请注明出处 http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Remove Nth Node From End of List的更多相关文章

  1. leetcode 题解 || Remove Nth Node From End of List 问题

    problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...

  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. 20_java之集合Map

    01Map集合概述 A:Map集合概述: 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同  a:Collection中的集合,元素是孤立 ...

  2. scrapy框架的持久化存储

    一 . 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 执行输出指定格式进行存 ...

  3. leetcode260

    public class Solution { public int[] SingleNumber(int[] nums) { var dic = new Dictionary<int, int ...

  4. RHCE7-学习里程 root 密码重置换

    一.root 密码重置换 当忘记root 密码后,可以按照以下方法重置 root 密码 1.重启动电脑,出现如下界面,按  上  下 键盘, 选择   (core)--再按e 2.在下图linux16 ...

  5. RocketMQ初探(一)

    初学RocketMQ,认识一门新技术,还是哪三问:是什么?能干什么?怎么用? 消息中间件主要是实现分布式系统中解耦.异步消息.流量销锋.日志处理等场景. Rocketmq是阿里捐赠给Apache的.3 ...

  6. selenium+python—实现基本自动化测试

    安装selenium 打开命令控制符输入:pip install -U selenium 火狐浏览器安装firebug:www.firebug.com,调试所有网站语言,调试功能 Selenium I ...

  7. Oracle-属性查询

    1. 查询表的部分字段属性 select t.*, c.comments from user_tab_columns t, user_col_comments c where t.table_name ...

  8. 循环结构之for循环

    循环结构之for循环(一) 在很多编程语言中都有一种直接.简单的循环,它的一般形式为: 它的执行过程如下: 第一步:执行表达式1,对循环变量做初始化: 第二步:判断表达式2,若其值为真(非0),则执行 ...

  9. Python之FTP传输-乾颐堂

    访问FTP,无非两件事情:upload和download,最近在项目中需要从ftp下载大量文件,然后我就试着去实验自己的ftp操作类,如下(PS:此段有问题,别复制使用,可以参考去试验自己的ftp类! ...

  10. Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密

    Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密 二.利用加密算法DES实现java代码加密 传统的C/C++自动带有保护机制,但java不同,只要 ...