File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ https://leetcode.com/problems/remove-nth-node-from-end-of-list/
2+
3+ 给一个链表,删除倒数第n个节点
4+
5+ ``` java
6+ class Solution {
7+ public ListNode removeNthFromEnd (ListNode head , int n ) {
8+ ListNode dummy = new ListNode (0 );
9+ dummy. next = head;
10+
11+ ListNode q = dummy, p = dummy;
12+ while (n-- > - 1 ){
13+ p = p. next;
14+ }
15+ while (p!= null ){
16+ q = q. next;
17+ p = p. next;
18+ }
19+ q. next = q. next. next; // 优化:释放被删除节点
20+ return dummy. next;
21+ }
22+ }
23+ ```
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ PS:除开知识点,一定要准备好以下套路:
4949* [ 数组-滑动窗口-最小连续子数组] ( https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-数组-滑动窗口-最小连续子数组.md )
5050* [ 数组-归并排序-合并有序数组] ( https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-数组-归并排序-合并有序数组.md )
5151* [ 链表-链表反转-链表相加] ( https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-链表-反转链表-链表相加.md )
52+ * [ 算法-链表-双指针-删除倒数第n个] ( https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-链表-双指针-删除倒数第n个.md )
5253* [ 二叉树-二叉树反转] ( https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-二叉树-二叉树反转.md )
5354* [ 动态规划-连续子数组最大和] ( https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-动态规划-连续子数组最大和.md )
5455* [ 数据结构-LRU淘汰算法] ( https://github.com/xbox1994/2018-Java-Interview/blob/master/MD/算法-数据结构-LRU淘汰算法.md )
You can’t perform that action at this time.
0 commit comments