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个节点。我想的是数一遍这个ListNode的长度,然后减去n,再删除。上代码:

public static ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null)return head; int length=0;
ListNode headCopy1=head;
ListNode headCopy2=head;
//calculate length
while(headCopy1!=null) {
length++;
headCopy1=headCopy1.next;
}
//remove first node,return head.next
if(n==length)return head.next; for(int i=0;i<(length-n-1);i++) {
headCopy2=headCopy2.next;
}
if(headCopy2.next.next==null) {
headCopy2.next=null;
}else {
headCopy2.next=headCopy2.next.next;
}
return head;
}

后面看了别人的思路,采用的是快慢指针,先在head前面加一个节点start,这个很重要,如果只有一个节点,且要删除这个节点,虽然可以直接返回null,但是在统一性上来说,就多了几行单独出来的代码,快慢指针都指向这个start。快指针先走,走n步,然后再让快慢指针一起走,直到快指针为null,这时候慢指针后面的即为要删除的,通过快慢指针实现了计算length-n,很巧妙。然后让next跳过一个节点即可。再有就是最后的返回值,一开始我以为返回head和start.next都可以,结果我用head来返回的时候就报错了。因为head始终指的是头节点,即使头节点删除了,也还是会返回值,而start这个节点是与返回值无关的一个节点,对start不会有任何操作,如果只有一个节点即head节点,且要删除这个节点,你再返回head就不对了,而返回start.next则为空。

public static ListNode removeNthFromEnd(ListNode head, int n) {
ListNode start=new ListNode(0);
start.next=head;
ListNode fast,slow;
fast=slow=start; for(int i=0;i<n+1;i++) {
fast=fast.next;
}
while(fast!=null) {
slow=slow.next;
fast=fast.next;
}
slow.next=slow.next.next;
return start.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 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. Django学习-19-缓存

    由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...

  2. 对维数组排序 array_multisort()的应用

    PHP允许在多维数组上执行一些比较复杂的排序--例如,首先对一个嵌套数组使用一个普通的关键字进行排序,然后再根据另一个关键字进行排序.这与使用SQL的ORDER BY语句对多个字段进行排序非常相似.为 ...

  3. RobotFramework自动化测试框架-移动手机自动化测试Open Application关键字的使用

    在AppiumLibrary库中,Open Application关键字用来打开一个待测试移动APP. 示例1:连接本机已经打开的appium服务端,打开一个待测试的安卓APP,指定测试平台为Andr ...

  4. Python Cookbook(第3版)中文版:15.20 处理C语言中的可迭代对象

    15.20 处理C语言中的可迭代对象¶ 问题¶ 你想写C扩展代码处理来自任何可迭代对象如列表.元组.文件或生成器中的元素. 解决方案¶ 下面是一个C扩展函数例子,演示了怎样处理可迭代对象中的元素: s ...

  5. 【BZOJ1415】【NOI2005】聪聪和可可(动态规划,数学期望)

    [BZOJ1415][NOI2005]聪聪和可可(动态规划,数学期望) 题面 BZOJ 题解 先预处理出当可可在某个点,聪聪在某个点时 聪聪会往哪里走 然后记忆化搜索一下就好了 #include< ...

  6. 【BZOJ3527】力(FFT)

    [BZOJ3527]力(FFT) 题面 Description 给出n个数qi,给出Fj的定义如下: \[Fj=\sum_{i<j}\frac{q_i q_j}{(i-j)^2 }-\sum_{ ...

  7. [POI2000]病毒

    题面 传送门 Sol 建出AC自动机后DFS能走的点,如果能走回来就可行 # include <bits/stdc++.h> # define IL inline # define RG ...

  8. Couldn't save uncommitted changes.

    关于idea切换分支报错的问题. Couldn't save uncommitted changes. Tried to save uncommitted changes in stash befor ...

  9. oracle数据库和表的操作

    一.字符函数 (1)连接符 concat --连接符 select concat('10086','-')||'1531234567' 电话号 from dual; (2)首字母大写 initcap ...

  10. Hive 自定义函数

    hive 支持自定义UDF,UDTF,UDAF函数 以自定义UDF为例: 使用一个名为evaluate的方法 package com.hive.custom; import org.apache.ha ...