[leetcode]_Remove Nth Node From End of List
题目:移除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的更多相关文章
- [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 ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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的 ...
- [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 ...
- leetcode remove Nth Node from End python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
随机推荐
- springMVC整合spring和hibernate4(适合于框架的搭建)
基础的东西不再详细说明,只在这里说明一下主要的配置文件,如何通过配置文件取得sessionFactory . 步骤: 1:在web.xml中引入springmvc的配置文件springmvc.xml( ...
- (medium)LeetCode 210.Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [POJ 2443] Set Operation (bitset)
题目链接:http://poj.org/problem?id=2443 题目大意:给你N个集合,每个集合里有若干个数.M个查询,每个查询有a,b两个数.问是否存在一个集合同时包含a,b这两个数.若存在 ...
- oracle rac 日志体系结构!
告警日志集群节点集群件告警日志:$GRID_HOME/log/<hostname>/alert<hostname>.log数据库实例的告警日志:$DIAG_DESTINATIO ...
- Memcached、Redis和MongoDB的区别
Memcached和Redis都是内存数据库. Memcached是多线程运行的: Redis单线程是单线程运行的: MongoDB是文档型的非关系型数据库..Net:RavenDB.
- sql 获取filename
select Substring(ORIGINAL_IMAGE,len(ORIGINAL_IMAGE)-charindex('/',reverse(ORIGINAL_IMAGE))+2,len(ORI ...
- 剑指Offer:面试题16——反转链表(java实现)
问题描述 定义一个函数,输入一个链表的头结点,反转该链表并输出反转后的链表的头结点.链表结点如下: public class ListNode { int val; ListNode next = n ...
- VC++中,如何定义callback函数和它的触发事件?
对于回调函数的编写始终是写特殊处理功能程序时用到的技巧之一.先介绍一下回调的使用基本方法与原理. 1.在这里设:回调函数为A()(这是最简单的情况,不带参数,但我们应用的实际情况常常很会复杂),使用回 ...
- g++/gcc 链接头文件 库 PATH
转自http://blog.csdn.net/kankan231/article/details/24243871 在Linux下编译链接或运行c/c++程序时可能会遇到找不到头文件,找不到库文件的错 ...
- maven多工程构建与打包
目标:webapp_aggregator为聚合和父pom工程,不包含代码和资源,webapp为主web工程,webapp_module1为子web工程,webapp_common为基础子工程,两个we ...