0
点赞
收藏
分享

微信扫一扫

LeetCode Top 100 Liked Questions 19. Remove Nth Node From End of List (Java版; Medium)


​​welcome to my blog​​

LeetCode Top 100 Liked Questions 19. Remove Nth Node From End of List (Java版; Medium)

题目描述

Given a linked list, remove the n-th node from the end of list and return its head.

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.

Follow up:

Could you do this in one pass?

class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next=head;
ListNode pre=dummy, cur=head, right=head;
for(int i=0; i<n-1; i++){
right = right.next;
}
while(right.next!=null){
pre = cur;
cur = cur.next;
right = right.next;
}
pre.next = cur.next;
return dummy.next;
}
}

第一次做, 一次遍历即可, 题目的基础就是找到链表中倒数第n个节点; 注意:不要让fast走到null,这样的话fast和slow得相隔k个节点,由于slow和fast的起点是head,所以相隔k个节点的话需要链表中有k+1个节点, 这样判断的话就麻烦了,比如链表只有k个节点,检查长度时fast走到了null,此时不能返回null,需要配合k进行判断,就变麻烦了。所以一步到位,让fast和slow相隔k-1个节点,不满足就说明链表长度小于k,返回null; 这道题的initialize, execute, decision不是传统模式

  • 之所以让fast走到最后一个节点而不是走到null, 是为了避免出现这样的情况: fast走到null时, 是正好走到null还是因为链表长度不够k? 出现了歧义

/*
这倒不一定先创建一个tempHead,因为处理头结点和非头结点不麻烦
要记得,删除某个节点的话, 得保存前一个节点
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null)
return null;
//initialize
ListNode pre=null, left=head, right = head;
for(int i=0; i<n-1; i++){
//execute
right = right.next;
//decision
if(right==null)
return null;
}
//循环终止条件也可以用right!=null,但要改动一下上面的for循环次数
while(right.next!=null){
//save last node
pre = left;
//update
left = left.next;
right = right.next;
}
//这里可以按照left是否等于head进行分类; 也可以按照pre是否为null进行分类; 二者是等价的
if(left==head)
return head.next;
pre.next = left.next;
return head;
}
}


举报

相关推荐

0 条评论