LeetCode 019 Remove Nth Node From End of List
来源:互联网 发布:java课程设计小游戏 编辑:程序博客网 时间:2024/05/17 01:24
题目
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 条件中,n永远合法,所以减少了错误判断所需的内容。
2 第一次last,先记录 离head n的节点在哪。
3 第二次,保留一个pre , pre 和 last同时前进,这样,当last指向最后一个节点的时候,pre正好是所要删除的节点的前一个节点。
4 进行删除操作。
public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { if(head==null){ return null; } ListNode dummy = new ListNode(Integer.MIN_VALUE); dummy.next = head; ListNode last =dummy; for(int i=0;i<n;i++){ last = last.next; } ListNode pre = dummy; while(last.next!=null){ last=last.next; pre=pre.next; } pre.next = pre.next.next; return dummy.next; }}
0 0
- LeetCode 019 Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List [019]
- 【LeetCode OJ 019】Remove Nth Node From End of List
- leetcode 019 Remove Nth Node From End of List (Python)
- 019 Remove Nth Node From End of List [Leetcode]
- LeetCode 019 Remove Nth Node From End of List
- [LeetCode]019-Remove Nth Node From End of List
- LeetCode 019 Remove Nth Node From End of List
- LeetCode 019 Remove Nth Node From End of List
- LeetCode-019 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: Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- leetcode 40: Remove Nth Node From End of List
- IOS开发的适配问题
- tomcat如何配置懒加载 每次更改不用重新启动服务器
- hdu1215
- EhCache WebCache 与 SpringMVC集成时 CacheManager冲突的问题
- 非管理员权限,没有选择记录
- LeetCode 019 Remove Nth Node From End of List
- OrangeUI For FireMonkey介绍
- Animation State Machine Preview (solo and mute)
- hadoop学习--单表关联
- Animation入门
- 浅谈拓扑排序
- MPC8313ERDB从内存启动Linux系统
- c++关于封装、接口的简单理解
- 数据库作业 大家忽略