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.

原题链接:https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/

题目:给定一个链表,从尾部删除第n个节点并返回新的链表。

思路:使用两个指针,fast 和 slow,他们的距离是n,于是fast到尾的时候,n所在的节点就是须要删除的节点。

	public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode slow = head, fast = head;
if (head.next == null)
return null;
for (int i = 1; i <= n; i++)
slow = slow.next;
if (slow == null) {
head = head.next;
return head;
}
while (slow.next != null) {
slow = slow.next;
fast = fast.next;
}
fast.next = fast.next.next;
return head;
}
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}

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. cocos2d中的anchorPoint属性详解

    原文地址:http://www.tuicool.com/articles/ANVjMj 1> anchorPoint对position的影响 anchorPoint的作用就是相当于确定在子节点的 ...

  2. mysql常见监控项

    1.MySQL服务运行状态 约定所有MySQL服务都必须以ip1(内网ip)来绑定,每个机器只有一个ip1,可以有多个端口,即多个MySQL Server.采集程序读取ip端口信息文件来判断serve ...

  3. LightOJ 1422 区间DP Halloween Costumes

    d(i, j)表示第i天到第j天至少要穿多少件衣服. 先不考虑第i天和后面 i+1 ~ j 天的联系,那就是至少要穿 1 + d(i+1, j)件衣服. 再看状态转移,如果后面第k(i+1 ≤ k ≤ ...

  4. RHEL6.5上升级OpenSSH7.4p1

    由于升级OpenSSH涉及到安全性问题,为保险起见,在升级前最好安装telnet服务作为备用方案,然后在升级成功后再停止telnet即可. 一.OpenSSH升级相关源码包下载地址 zlib  htt ...

  5. 【05】Number图解

    [05]Number图解  

  6. OSPF 一 基础

    本节介绍ospf路由选择协议    为链路状态  路由选择协议 一  分类 open shortest path first   开放最短路优先   公有协议 单区域的ospf实施  运行在一个自治系 ...

  7. VS2010SP1修复补丁&Microsoft Visual Studio 2010 Service Pack 1

    网上比较难找,官网找也容易找错,现在贴出来 补丁包下载地址:链接:https://pan.baidu.com/s/1_tFzXL6PaHiWk3JeRBw0ww 密码:z38k

  8. 【UML】UML所扮演的角色(视频总结)

    导读:在国庆中,把UML视频看完了.看完了之后,对于自己到底留下了什么呢,在此就总结一下,前面总结了UML的9种图以及主要的关系,本篇博客,就从整体上对UML做一个说明. 一.总体概述 UML一共讲了 ...

  9. CSS相对布局和绝对布局

    relative 相对布局,正常的,从上到下.绝对布局absolute,就像不占位置,透明了一样,会和别的重合

  10. 【Luogu】P1868饥饿的奶牛(DP)

    题目链接 话说我存一些只需要按照一个关键字排序的双元素结构体的时候老是喜欢使用链式前向星…… DP.f[i]表示前i个位置奶牛最多能吃到的草.转移方程如下: f[i]=f[i-]; f[i]=max( ...