目录

  • 题目链接
  • 注意点
  • 解法
  • 小结

题目链接

Remove Nth Node From End of List - LeetCode

注意点

  • 考虑删除的结点是开头的那个结点
  • 输入给的链表是没有开头的"哑结点"的

解法

解法一:先在开头加一个哑结点,这样可以简化后面的处理。一次遍历统计有多少个结点。然后再一次遍历找到倒数第n个结点,修改next指针,结束循环。时间复杂度为O(n)

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* removeNthFromEnd(ListNode* head, int n) {ListNode* ans = new ListNode(0);ans->next = head;ListNode* p = ans;ListNode* q = ans->next;int count = 0;while(q != NULL){q = q->next;count++;}int stop = count-n+1;count = 1;q = ans->next;while(q != NULL){if(count == stop){p->next = q->next;break;}p = q;q = q->next;count++;}return ans->next;}
};

解法二:只需要一次遍历,两个指针p和q,首先p先走n步,然后p和q一起走,当p走到结尾的时候q就走到倒数n+1个结点了。时间复杂度为O(n)

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* removeNthFromEnd(ListNode* head, int n) {ListNode* p = head;ListNode* q = head;while(n > 0){p = p->next;n--;}if(p == NULL)//remove first node{return q->next;}while(p->next != NULL){q = q->next;p = p->next;}q->next = q->next->next;return head;}
};

小结

  • 解法二还是很精妙滴

转载于:https://www.cnblogs.com/multhree/p/10345974.html

Remove Nth Node From End of List - LeetCode相关推荐

  1. Remove Nth Node From End of List leetcode java

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

  2. LeetCode算法入门- Remove Nth Node From End of List -day17

    LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释: Given a linked list, remove the n-th nod ...

  3. LeetCode:Remove Nth Node From End of List 移除链表倒第n项

    2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...

  4. LeetCode 19. Remove Nth Node From End of List

    LeetCode 19. Remove Nth Node From End of List Solution1:我的答案 并不算是最优解法. /*** Definition for singly-li ...

  5. 数据结构与算法 | Leetcode 19. Remove Nth Node From End of List

    原文链接:https://wangwei.one/posts/jav... 前面,我们实现了 两个有序链表的合并 操作,本篇来聊聊,如何删除一个链表的倒数第N个节点. 删除单链表倒数第N个节点 Lee ...

  6. LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素

    前言 本文是LeetCode19. Remove Nth Node From End of List解法,这个题目需要删除链表中的倒数第n个位置的元素 代码 # -*- coding: utf-8 - ...

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

  8. LeetCode上删除链表末尾第N个节点算法——Remove Nth Node From End of List

    1.题目 Given a linked list, remove the n-th node from the end of list and return its head. Example: Gi ...

  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. java 鼠标拖动图形_java怎么实现鼠标在桌面拖动过程中 画 矩形的 功能?
  2. 启动mysql会遇到的问题_MySQL学习(一)——启动和登录MySql遇到的问题及解决
  3. 调用startActivityForResult后,onActivityResult无响应的题目
  4. 领域驱动设计,让程序员心中有码(八)
  5. jupyter notebook使用入门2——创建一个基于scikit-Learn的线性预测ipynb文件
  6. 英语总结系列(二十七):重复就是力量
  7. clob和blob是不是可以进行模糊查询_SQL简单查询语、运算符学习和练习
  8. python 爬虫性能_python-爬虫性能相关
  9. 推荐两个非常不错的公众号
  10. Ubuntu System Panel:Ubuntu 系统的新概念菜单
  11. 计算年龄:DATEDIF函数
  12. mysql 判断质数_质数(素数)判断算法总结
  13. 中国最美的十大宗教名山(图)
  14. 搭建分布式FastDFS集群
  15. html表格标题标签_HTML标题标签
  16. 网络socket编程--多路复用
  17. 专访|十年程序员董一凡:生命不息,学习不止
  18. 地址转为经纬度通过DBSCAN进行关联识别
  19. 机器学习小组知识点31:重要性采样(Importance Sampling )
  20. 优秀github博主

热门文章

  1. Hibernate学习5—Hibernate操作对象
  2. 关于Java重载方法匹配优先级
  3. HTML5实践之歌词同步播放器
  4. .中英文系统底层编码导致乱码问题
  5. div+css控制最小高度又自适高度
  6. Install 802.1x In Fedora
  7. Samba的配置命令
  8. GitLab 密码重设
  9. pywin32的安装
  10. 新建arcgis api for android 项目失败