1 题目

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.

2 思路

题目要求遍历一遍搞定,那么,就可以用两个指针,先第一个先往前走N个,然后再一起走,最后,移除后一个元素。自己做出来的。。

3 代码

        public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode node = head;
while(n>0 && node!=null){
node = node.next;
n--;
}
ListNode start = head;
if(node != null){
while(node.next != null){
node = node.next;
start = start.next;
}
}else{
/* will remove the first object */
start = start.next;
return start;
} start.next = start.next.next;
return head;
}

[leetcode 19] Remove Nth Node From End of List的更多相关文章

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

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

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

  4. (链表 双指针) leetcode 19. Remove Nth Node From End of List

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  5. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  6. 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]

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

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

  8. [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  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. 【转】关于LWF——线性工作流

    1.什么是LWF? LWF全称Linear Workflow,中文翻译为线性工作流.“工作流”在这里可以当作工作流程来理解.LWF就是一种通过调整图像Gamma值,来使得图像得到线性化显示的技术流程. ...

  2. knockoutJS+knockout.multimodels使用记录

    可以多次绑定,但不能嵌套绑定 错误示例: <div class="con_c" data-model="viewModel"> <div da ...

  3. 开启telnet的几种方法

    开启telnet方法一:需要VTY的密码和进入超级权限的密码(VTY虚拟终端,一种网络设备的连接方式) [R1]int g0/0/0[R1-GigabitEthernet0/0/0]ip add 19 ...

  4. android系统中查看哪些端口被哪些应用打开

    1 查看哪些端口开放,netstat 2 根据端口号获取到UID,比如端口号为10050,转成16进制是2742,使用命令grep -i 2742 /proc/net/tcp6,就能看到其UID,假如 ...

  5. DropDownList怎样动态绑定数据库中的某一条数据

    用Ajax动态绑定数据库的数据:点击后台查看代码,编写代码如下 if (!IsPostBack) { using (SnailTechDataContext con = new SnailTechDa ...

  6. JAVA课程实验报告 实验三 敏捷开发与XP实践

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计  班级:1353  姓名:韩玉琪  学号:20135317 成绩:             指导教师:娄嘉 ...

  7. js中函数的定义

  8. mysql.sock的作用

    1.在编译安装mysql的时候,会将mysql的配置文件复制到/etc/my.conf中: [root@Web-lnmp02 mysql]# cp support-files/my-small.cnf ...

  9. LightOJ 1313 - Protect the Mines(凸包)

    1313 - Protect the Mines   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 M ...

  10. Android支付之支付宝封装类

    今天介绍下在android中如何集成支付宝支付到自己的APP中去.让APP能够拥有方便,快捷的支付功能. 我们在做Android支付的时候肯定会用到支付宝支付,根据官方给出的demo做起来非常费劲,所 ...