[LeetCode]Remove Nth Node From End of List
又一道链表题。CareerCup上也有类似题目,trick和“部分链表逆序”一样,由于不知道链表的长度,second指针比first指针先走n步,然后在同时向前移动,直到second指针到达链表尾。注意删除头节点的情况。
假设链表长度为N,倒数第k个节点是正数第N-k个节点(k<=N),前驱指针p指向第N-k-1个节点,后向指针q指向第N个节点。
Given a linked list, remove the nth node from the end of list and return its head.
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.
代码
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *removeNthFromEnd(ListNode *head, int n) { 12 // Note: The Solution object is instantiated only once and is reused by each test case. 13 if(head == NULL) 14 return head; 15 16 ListNode *p = NULL; 17 ListNode *q = head; 18 for(int i=1; i<n; ++i) 19 q = q->next; 20 while(q->next!=NULL) 21 { 22 if(p) 23 p = p->next; 24 else 25 p = head; 26 q = q->next; 27 } 28 if(p) 29 { 30 ListNode *tmp = p->next; 31 p->next = tmp->next; 32 delete tmp; 33 } 34 else 35 { 36 ListNode *tmp = head; 37 head = head->next; 38 delete tmp; 39 } 40 41 return head; 42 } 43 };
转载于:https://www.cnblogs.com/practice/p/3382434.html
[LeetCode]Remove Nth Node From End of List相关推荐
- 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, ...
- 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, ...
- LeetCode Remove Nth Node From End of List
题意:给出一个单链表,删除从尾部开始的第n个元素 思路: 从头开始遍历n个元素,此时当前结点指向头开始的第n+1个元素.继续遍历同时,另外一个结点记录从头开始至遍历结束时的所经历的结点,这个结点就是要 ...
- Remove Nth Node From End of List - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Remove Nth Node From End of List - LeetCode 注意点 考虑删除的结点是开头的那个结点 输入给的链表是没有开头的& ...
- 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:Remove Nth Node From End of List 移除链表倒第n项
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...
- 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 ...
- LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素
前言 本文是LeetCode19. Remove Nth Node From End of List解法,这个题目需要删除链表中的倒数第n个位置的元素 代码 # -*- coding: utf-8 - ...
最新文章
- C语言的单链表求交点
- webpack2--webpack 4.X 快速创建demo
- 用shell脚本监控进程是否存在 不存在则启动的实例
- wex5 php开发,WeX5平台PHP开发 使用说明文档
- Spring Data JPA的持久层
- LNK2019 无法解析的外部符号 __imp_CommandLineToArgvW,该符号在函数 WinMain 中被引用
- cuda pytorch 环境变量_PyTorch-GPU环境配置
- Selecting Foreground or Background Colors
- python语法学习第九天--else和with语句
- java计算机毕业设计教育机构管理源码+mysql数据库+系统+lw文档+部署
- 208个地级市和31个省市城乡泰尔指数(2010-2019年)
- mysql查询登录端口_mysql查看、修改端口、指定端口登录
- wincc显示系统时间_Wincc的系统时间该如何用变量显示?
- Android模拟器的安装、连接和操作
- Solaris下网卡绑定多个IP
- 新一代 IT 服务管理平台 DOSM,助力企业数字化转型
- 还在用Evernote或印象笔记吗?来看看笔记神器Notion吧!
- css炫酷标题,分享几个CSS小众但炫酷的技巧
- 利用python将excle表格由xls转换为xlsx格式
- jackson-databind 版本升级遇到的问题