Remove Nth Node From End of List --移除链表中的倒数第k个元素
来源:互联网 发布:知乎华为软件开发面试 编辑:程序博客网 时间:2024/06/12 11:22
问题:链接
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+1个元素,移除它后面的元素即可。
代码:
class Solution {public: ListNode *removeNthFromEnd(ListNode *head, int n) { int len=0; ListNode *pf = head; while(pf != NULL) { ++len; pf = pf->next; } if(len == n) return head->next; pf = head; ListNode *pl = head; for(int i = 1; i <= n && pf->next != NULL; i++) pf = pf->next; while(pf->next != NULL) { pf = pf->next; pl = pl->next; } pl->next = pl->next->next; return head; }};
0 0
- Remove Nth Node From End of List --移除链表中的倒数第k个元素
- Remove Nth Node From End of List "移除链表中的倒数第N项"
- Remove Nth Node From End of List 链表删除倒数第N个元素@LeetCode
- Leetcode Remove Nth Node From End of List 删除链表倒数第n个元素
- leetcode 3. 移除链表的倒数第n个节点 Remove Nth Node From End of List
- 19. Remove Nth Node From End of List (移除链表倒数第n个节点)
- 19. Remove Nth Node From End of List(移除链表的倒数第n个节点)
- LeetCode--Remove Nth Node From End of List(移除链表的倒数第N个节点)Python
- LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点
- LeetCodet题解--19. Remove Nth Node From End of List(删除链表的倒数第n个元素)
- LeetCode[19]Remove Nth Node From End of List 删除链表倒数第n个元素
- 删除链表的倒数第n个结点 Remove Nth Node From End of List
- Remove Nth Node From End of List 删除链表的倒数第n个结点
- LeetCode(Remove Nth Node From End of List)删除链表倒数第n个节点
- Remove Nth Node From End of List 删除链表倒数第N个节点
- 【链表&删除倒数第K个节点】Remove Nth Node From End of List
- LeetCode | Remove Nth Node From End of List(移除链表中倒数第n个结点)
- 19.Remove Nth Node From End of List(移除单链表中倒数第N个结点)
- C++游戏开发需要阅读的书籍 (转)
- TCP编程(二)
- Remove Duplicates from Sorted List --已出有序链表中的重复元素
- 选择排序和冒泡排序
- DOS命令将黑框中查询到的信息保存到TXT等文件中
- Remove Nth Node From End of List --移除链表中的倒数第k个元素
- andriod API文档打不开 google打不开
- 进制转换问题
- Java的串行化
- 无向图的连通分支
- Palindrome Number --回文整数
- 配置与管理NFS网络文件系统
- 黑马程序员-day12-多线程(线程间通信)
- python读文件