Remove Nth Node From End of List —— Leetcode
来源:互联网 发布:小世界网络 聚类系数 编辑:程序博客网 时间:2024/05/21 22:28
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.
Note:
Given n will always be valid.
Try to do this in one pass.
题目很容易理解,即删除倒数第n个结点,注意以下两个地方:
(1)要求只遍历一次,一个指针不行的情况下,要想到用两个指针;
(2)链表删除问题边界情况都有头结点的问题,所以要注意这里的边界情况(注释地方为第一次提交写错的地方)
C代码:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode *removeNthFromEnd(struct ListNode *head, int n) { struct ListNode *i=head, *j=head, *pi=head; for(int k=1; k<n; k++) j = j->next; while(j->next != NULL) { pi = i; i = i->next; j = j->next; } if(i == head) // not if(pi == head) { head = head->next; pi->next = NULL; } else { pi->next = i->next; i->next = NULL; } return head;}
0 0
- LeetCode——Remove Nth Node From End of List
- Remove Nth Node From End of List —— Leetcode
- leetcode—Remove Nth Node From End of List
- LeetCode19——Remove Nth Node From End of List
- <19>——Remove Nth Node From End of List
- Algorithms—19.Remove Nth Node From End of List
- LeetCode[Linked List]: Remove Nth Node From End of List
- 19. Remove Nth Node From End of List leetcode list
- 【LeetCode】LeetCode——第19题:Remove Nth Node From End of List
- [Leetcode]LeetCode 19:Remove Nth Node From End of List
- leetcode——19——Remove Nth Node From End of List
- Python学习——leetcode(Remove Nth Node From End of List)
- leetcode 019 —— Remove Nth Node From End of List
- LeetCode题解——Remove Nth Node From End of List
- leetcode刷题日记——Remove Nth Node From End of List
- leetcode第19题——*Remove Nth Node From End of List
- LeetCode OJ刷题历程——Remove Nth Node From End of List
- LeetCode——19. Remove Nth Node From End of List
- jquery中的 delegate 可以让 代码中动态加入td元素,同样会被赋予单击事件!
- eclipse默认指向WebContent目录修改为webRoot 设置说明
- java.util.concurrent包API学习笔记
- 线性布局下,如何最左或者最右
- repaint,update和paint方法的区别
- Remove Nth Node From End of List —— Leetcode
- 数据结构链式表实验报告
- 将一个或多个空格替换成逗号“,”
- word如何转换成PDF的方法
- jquery 新添加的 tr td 没有办法执行click 事件
- QT中消息处理机制
- Linux中常用的压缩和解压缩命令汇总
- 奇怪的Linq问题
- 语法错误,仅当源级别为 5.0 时注释才可用