[leetcode]_Remove Nth Node From End of List

时间:2023-02-18 10:11:15

题目:移除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. &lbrack;LeetCode&rsqb; 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&colon; Remove Nth Node From End of List 解题报告

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

  3. &lbrack;leetcode&rsqb;Remove Nth Node From End of List &commat; 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. &lbrack;LeetCode&rsqb; 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. &lbrack;Leetcode&rsqb; 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. Maven仓库 国内镜像

    <repositories> <repository> <id>repo-mirror</id> <url>http://maven.net ...

  2. 学堂在线 UWP 首版

    好久没有写博客了,主要是最近在写一个小小的App.<( ̄︶ ̄)> 不知道看各位有木有爱看慕课的,作为一名资深的大三学渣的我有看慕课的习惯.一直在看学堂在线的慕课,感觉质量确实还可以,但是遗 ...

  3. hdu2121 - Ice&lowbar;cream’s world II&lpar;朱刘算法&comma;不固定根&rpar;

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...

  4. URAL 1775 B - Space Bowling 计算几何

    B - Space BowlingTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  5. 【转】如何在vmware中如何设置ip

    如何在vmware中如何设置ip 1.修改网络接口选hostonly2.虚拟机里安装vmware-tool,对鼠标和图形进行更好地支持.如果你在图形界面下,首先要切换到文本模式.右键点击桌面,打开一个 ...

  6. SunTlsRsaPremasterSecret KeyGenerator not available问题解决

    本地Idea测试可以,部署到开发环境遇到下面的问题: javax.net.ssl.SSLKeyException: RSA premaster secret error at com.sun.net. ...

  7. dos choice 命令

    dos choice 命令 choice /c YNC /m "Are you sure?" if %errorlevel%==1 goto Y if %errorlevel%== ...

  8. AbstractQueuedSynchronizer-AQS

    AbstractQueuedSynchronizer,这个类是整个java.util.concurrent的核心之一. CountDownLatch 能够使一个或多个线程等待其他线程完成各自的工作后再 ...

  9. LevelDB源码分析-MemTable

    MemTable(db/memtable.h db/memtable.cc db/skiplist.h) LevelDB中存储在内存中的那部分KV数据都存储在memtable中,而memtable中的 ...

  10. 【转】WPF自定义控件与样式&lpar;8&rpar;-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 下拉选择控件ComboBox的自定义样式及扩展: 自定义多选控件Mul ...