Lintcode174 Remove Nth Node From End of List solution 题解
来源:互联网 发布:论坛模板源码 编辑:程序博客网 时间:2024/05/22 04:51
【题目描述】
Given a linked list, remove the nth node from the end of list and return its head.
【注】The minimum number of nodes in list is n.
给定一个链表,删除链表中倒数第n个节点,返回链表的头节点。
【注】链表中的节点个数大于等于n
【题目链接】
www.lintcode.com/en/problem/remove-nth-node-from-end-of-list/
【题目解析】
此题可用双指针来解决。
首先让faster从起始点往后跑n步。再让slower和faster一起跑,直到faster==null时候,slower所指向的node就是需要删除的节点。
注意,一般链表删除节点时候,需要维护一个prev指针,指向需要删除节点的上一个节点。
为了方便起见,当让slower和faster同时一起跑时,就不让 faster跑到null了,让他停在上一步,faster.next==null时候,这样slower就正好指向要删除节点的上一个节点,充当了prev指针。这样一来,就很容易做删除操作了。
slower.next = slower.next.next(类似于prev.next = prev.next.next)。
同时,这里还要注意对删除头结点的单独处理,要删除头结点时,没办法维护prev节点,所以当发现要删除的是头结点时,直接让head = head.next并return head即可。
【参考答案】
www.jiuzhang.com/solutions/remove-nth-node-from-end-of-list/
- Lintcode174 Remove Nth Node From End of List solution 题解
- 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题解 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List题解
- leetcode题解-19. Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node from End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- 648. Replace Words
- wireshark使用抓包详细图文教程
- xamarin学习笔记B06(ios的Autolayout布局)
- oracle12c plsql cann't access
- Servlet 工程 web.xml 中的 servlet 和 servlet-mapping 标签
- Lintcode174 Remove Nth Node From End of List solution 题解
- wireshark过滤MAC地址/物理地址
- 揭开“流量劫持”的神秘面纱:月入至少三万
- linux 下创建和删除软连接
- 复制特定div中的文字
- InputConnection模拟文字输入
- Canvas绘制星球轨迹移动
- Mail Chimp
- Linux tcpdump命令详解