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一定是有效的,限定一次遍历解决问题。首先定义两个指针 first 和 last,first 指向第一个节点,last 指向第 n+1 个节点;两个节点同时向后撸,直到 last.next 为 null 的时候,说明当前 first.next 即为待删除节点,直接 first.next.next = first.next 即可删除,然后返回 head。注意,如果一开始的时候 last 就为 null,说明 head 节点就是带删除的节点,直接返回 head.next 即可。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode first = head;
ListNode last = head;
while (n-- > 0) {
last = last.next;
} if (last == null) {
return head.next;
} while (last.next != null) {
first = first.next;
last = last.next;
} first.next = first.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. [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, ...

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

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

  5. (链表 双指针) 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 ...

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

  7. 蜗牛慢慢爬 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 ...

  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. 水仙花数---基于python

    # coding:utf-8"""水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153) ...

  2. POJ 3693 Maximum repetition substring(后缀数组)

    Description The repetition number of a string is defined as the maximum number R such that the strin ...

  3. UVALive 3668 A Funny Stone Game(博弈)

    Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2,...,  ...

  4. Python—文件

    def fileCopy(src, dst, srcEncoding, dstEncoding): with open(src, 'r', encoding=srcEncoding) as srcfp ...

  5. LintCode-41.最大子数组

    最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2 ...

  6. hive mapjoin优化

    默认为10MB,如果大于该值不会执行mapjoin,hive语句中直接设置的mapjoin也不再起作用. 参考hive wiki把hive.auto.convert.join.noconditiona ...

  7. 【week4】课堂Scrum站立会议

    项目:连连看游戏 小组名称:天天向上(旁听) 小组成员:张政 张金生 李权 武致远 已完成任务 1.本项目采用c#. 2. 初步界面. 形成一个windows下的游戏界面,每个需要消除的方块是一个bu ...

  8. python爬虫从入门到放弃(五)之 正则的基本使用(转)

    什么是正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是 事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符”,这个“规则字符” 来表达对字符的一种过滤逻辑. 正则并不是pyth ...

  9. 关闭win7/Server 2008非正常关机启动自动修复功能

    命令提示符下输入 bcdedit /set {default} bootstatuspolicy ignoreallfailures bcdedit /set {current} recoveryen ...

  10. 同步锁(synchronized)使用三要素

    1.代码被多个线程访问 2.代码中有共享的数据 3.共享数据被多个语句操作