视频讲解  http://v.youku.com/v_show/id_XMTY1MTMzNjAyNA==.html

(1)定义两个指针

ListNode fast = head;

ListNode slow = head;

(2)将快指针向前移动N步

(3.1)判断此时快指针是否已经到达尽头,如果是,头节点就是要删除的节点,返回head.next。

(3.2)将快慢两个指针同时以相同的速度往前移动,当快指针走到尽头的时候,慢指针的下一个位置就是倒数第N个节点,将慢指针next指向next.next.

public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) { ListNode fast = head;
ListNode slow = head; for(int i=0;i<n;i++){
fast = fast.next;
} if(fast == null){
head = head.next;
return head;
} while(fast.next != null){
fast = fast.next;
slow = slow.next;
} slow.next = slow.next.next;
return head;
}
}

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

  1. Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...

  2. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

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

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

  4. Merge Two Sorted Lists & Remove Nth Node From End of List

    1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...

  5. leetcode-algorithms-19 Remove Nth Node From End of List

    leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...

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

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

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

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

  9. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

随机推荐

  1. IOS OC 计算器算法(不考虑优先级)

    个人见解:为还在计算器算法方面迷惑的同学一个数据处理解决方案:定义一个可变数组array,一个可变字符串str,使字符通过[array addObject:str];方法添加到可变数组,每当触发运算符 ...

  2. mysqli连接数据库的模板

    <?php $host="localhost"; $db_user="root"; //数据库用户 $db_pass=""; //数据 ...

  3. mysql-python

    sudo pip install MySQL-python centos安装 python-dev包提示No package python-dev available: 出现此问题的原因是python ...

  4. 史上最全的Linux常用命令

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  5. Java导包——import语句

    使用语句import org.common.demo01.Demo: 或者import org.common.demo01.*: 如果一个类声明为public class,则文件名称必须与类名称一致, ...

  6. win7或win2008 R2 被远程登录日志记录 系统日志

    事件查看器 → Windows 日志 → 安全 (win7 事件查看器 打开方式 :计算机 右键   → 管理  → 计算机管理 → 系统工具 → 事件查看器 windows server 2008 ...

  7. 隔离click事件

    有一些应用,不需要我们自己的定义的click函数,例如: $(document).on('click', '#inp', function(e){ alert('hello world!'); }); ...

  8. ERROR 1130 (HY000) Host ‘hostname’ is not allowed to connect to this MySQL server

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'; FLUSH PRIVILEGES;

  9. macbook 放flash发烫,转html5

    http://zythum.sinaapp.com/youkuhtml5playerbookmark/

  10. 【phpcms-v9】phpcms-v9二次开发所必须知道的步骤(转载)

    一.做phpcms-v9二次开发时,我们经常需要用到如下代码,所以有必须在这里注释说明一下 defined('IN_PHPCMS') or exit('No permission resources. ...