【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,
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.
题意:给出一个链表,删除倒数第n个节点
刚在书上见过这个题,思路是用双指针,p1,p2,先让p2走n步,然后p1,p2一块走,当p2停止的时候,
p1->next正好指向要删除的节点,不过要注意:
当p2停止的时候,n还没变为0,这时候要验证n的值,当n>0时候,p1指向的节点就是要删除的节点
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * struct ListNode *next; 6 * }; 7 */ 8 struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { 9 if(head==NULL) 10 return head; 11 struct ListNode *p1,*p2,*tmp; 12 p1=head; 13 p2=head; 14 while(n>0&&p2->next!=NULL){ 15 p2=p2->next; 16 n--; 17 } 18 if(n>0){ 19 tmp=p1; 20 head=p1->next; 21 free(tmp); 22 } 23 else 24 { 25 while(p2->next!=NULL){ 26 p2=p2->next; 27 p1=p1->next; 28 } 29 tmp=p1->next; 30 if(tmp!=NULL){ 31 p1->next=tmp->next; 32 free(tmp); 33 } 34 } 35 return head; 36 }
转载于:https://www.cnblogs.com/fcyworld/p/6215692.html
【LeetCode】19. Remove Nth Node From End of List相关推荐
- 【Leetcode】19. Remove Nth Node From End of List (cpp)
讲思路的文章已经很多了,不再赘述,这里用的方法是两个指针一次遍历算法.想强调的是有可能要删除的就是第一个结点.删除头节点比较方便的做法是加一个伪头节点. /*** Definition for sin ...
- LeetCode 19. Remove Nth Node From End of List
LeetCode 19. Remove Nth Node From End of List Solution1:我的答案 并不算是最优解法. /*** Definition for singly-li ...
- 数据结构与算法 | Leetcode 19. Remove Nth Node From End of List
原文链接:https://wangwei.one/posts/jav... 前面,我们实现了 两个有序链表的合并 操作,本篇来聊聊,如何删除一个链表的倒数第N个节点. 删除单链表倒数第N个节点 Lee ...
- 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 ...
- leetcode 19. Remove Nth Node From End of List | 19. 删除链表的倒数第 N 个结点(Java)
题目 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 题解 首先,找到倒数第 n 个节点的前一个节点 pre. 然后,令 ...
- 19.Remove Nth Node From End of List
方法1:由于链表不能随机访问,所以很自然的想法是第一遍得到链表长度,然后计算倒数第n个结点的位置,但这样时间复杂度O(n2),想到用空间换取时间,可以用一个地址数组存储每个结点的地址,然后直接删除掉倒 ...
- 【Leetcode】2423. Remove Letter To Equalize Frequency
题目地址: https://leetcode.com/problems/remove-letter-to-equalize-frequency/description/ 给定一个长nnn字符串sss, ...
- LeetCode:Remove Nth Node From End of List 移除链表倒第n项
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...
- 【leetcode】
1. Two Sum [题目]https://leetcode.com/problems/two-sum/description/ [思路]将数组 利用 map 处理 即可 [代码] 1 class ...
最新文章
- 题库练习1(单词长度、统计字符个数、)
- 【bzoj3442】学习小组 费用流
- 张亚勤:PC之外的争夺战
- 给地球上的22.8万种植物建立图像索引
- 印度 语言简称_保存印度的语言和文化:图卢维基百科的诞生
- Python的pip下载特定版本TensorFlow方法
- pythonnumpy矩阵详解_python常用模块numpy解析(详解)
- 汇编语言-王爽 随书源码、检测点、实验答案
- html编写个人博客_云开发平台开箱,3分钟零基础搭建个人Hexo博客
- C语言指针 与字符串的学习
- Redfish Data model (红鱼的资料模型)
- Python中单引号,双引号,3个单引号及3个双引号的区别
- JavaScript 严格模式(strict mode)
- 以太坊大厦将倾?老白:EOS不会取代以太坊,各有应用场景
- 关于Servlet的两种配置Web.xml文件配置或者使用(@WebServlet(name = ,urlPatterns = ))配置问题——WebServlet注解
- 分享职场心得《14》
- 普通麦克风和多阵列麦克风的区别
- Wannafly挑战赛14 A-直角三棱锥
- 6、python-mongodb获取数据存储到sql server
- windows10删除英语(UK英国),英语(EU美国)键盘
热门文章
- linux 内存使用原理,linux中内存使用原理
- pdo 参数化查询 mysql函数_PDO笔记之参数化查询
- php 获取sql字段名,php – 使用DISTINCT和UNION在SQL查询中获取字段名称
- leetcode怎么用时间刷_刷完700多题后的首次总结:LeetCode应该怎么刷?
- netapp管理地址_NetApp常用管理命令总结
- 微信多开txt_电脑版微信怎么双开、多开
- poll函数_I/O复用 - 三组I/O复用函数的比较
- appcan php图片上传,appcan文件上传php,asp通用
- h3c交换机 查看二层交换机端口ip_【分享】项目中如何选到称心如意的交换机?...
- 【C++grammar】析构、友元、拷贝构造函数、深浅拷贝