题目:移除linked-list从尾到头的第N个元素

自我思路:因为题目给出的N是从链表尾开始计算的,单链表不存在从子指向父亲的反向指针,因此我先计算链表的整个长度len,然后用len - N来表示正向被删除元素所在的位置。

代码:

public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null) return null; ListNode help = head;
int length = 1;
while(help.next != null){
length++;
help = help.next;
} if(n == length){
head = head.next;
}else{
ListNode temp = head;
for(int i = 1 ; i < length - n ; i++) temp = temp.next; if(n == 1) temp.next = null;
else temp.next = temp.next.next;
}
return head;
}

这样的算法有个很低效的地方,如果我要删除一个very very 长的链表的倒数第二个元素,我得先遍历一遍,这一步可能半小时过去了。然后我又得查找一遍,才能删除。

网络上的思路非常精辟,双指针(快指针 & 慢指针),虽然原理还没有很理解,但是举例这样的确能得到正确答案。快指针比慢指针先走N步,N步以后快慢指针同时继续走,当快指针走到链表尾部时,慢指针指向的就是被删除元素的前一个元素。精妙的思路。

public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null) return null; ListNode fast = head;
ListNode slow = head; int fastGo = n;
while(n > 0 && fast.next != null){
fast = fast.next;
n--;
} if( n == 0 ){
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
if(slow.next.next != null) slow.next = slow.next.next;
else slow.next = null;
}else{
head = head.next;
}
return head;
}

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

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

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

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

  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 End of List 快慢指针

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

  7. LeetCode OJ-- Remove Nth Node From End of List

    https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ remove倒数第n个节点 一般list remove node的 ...

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

  9. leetcode remove Nth Node from End python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

随机推荐

  1. 第一个jave程序-helloworld

    1.打开myeclipse,其中有个select a workspase的过程,即选择工作空间,这里需要更换空间,不要放C盘,防止项目越来越大占用C盘的空间 2.创建java工程 3.取工程名,填写自 ...

  2. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

  3. 为textarea增加maxlength属性(转)

    如果只是單純地想限制 textarea 中的字數,不想寫太多的話,可用:   <textarea onkeyup="this.value = this.value.slice(0, 8 ...

  4. 剑指Offer:面试题12——打印1到最大的n位数(java实现)

    问题描述: 输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的3位数即999. 思路1:最简单的想法就是先找出最大的n位数,然后循环打印即可. public ...

  5. jmeter随笔(2)--上传接口报错

    黑夜小怪(2016-8-24  23:45) 微信订阅号: 问题:今天同事遇到问题,一个图片上传接口,单独跑是ok的,但是放在和其他接口一起就跑不通,如图 分析:查看该接口fiddler的抓包,发现请 ...

  6. 性能测试脚本新玩法---fiddler&&Jmeter

    飞测说:最近接触移动项目,测试app,需要做移动app的性能测试,想通过代理来录制,但是jmeter的代理录制效果真心不是很好,自己一个个请求来写代码,太浪时间了,那么有没有其他的办法呢? 我们都知道 ...

  7. ORA-01461: 仅可以为插入 LONG 列的 LONG 值赋值”解决办法

    ORA-01461: 仅可以为插入 LONG 列的 LONG 值赋值”解决办法 今天遇到的一个问题: 直接修改数据表中的某些字段数据内容时报错: ORA-01461: 仅可以为插入 LONG 列的 L ...

  8. easyui combo下拉框多选框

    按照自己的方式,先晒下效果图: 选一个值,那么就在input里面显示一个,去掉勾选,那么input就会少一个 其实做法很简单,今天就是快下班了,闲着没事加篇博客而已,下面带上代码. 1.页面的展示,i ...

  9. Jackson 框架,轻易转换JSON(转)

    Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 相比json-lib框架,Jackson所依赖的jar包较少,简单易用并且性能也 ...

  10. python基础(set)补充

    1.函数参数(引用)  函数的传参,传的是引用 def func(args): args.appand(123) li=[11,22,33] func(li) print(li) [11,22,33, ...