Leetcode 19 Remove Nth Node From End of List (快慢指针)

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.

题目解析:

依旧还是快慢指针, 思想是: 两个node, fast先走n步, 然后两个node一起走, 当fast走到list结尾的时候, lower刚好在要删除的node的前面. 之后就是lower.next = lower.next.next;就行啦~

注意几种情况要判断, 不然会time error:

  1. n = 0 || head == null
  2. n == 1 && head.next == null
  3. 删除head节点的时候

上代码:

 1 public ListNode removeNthFromEnd(ListNode head, int n) {
 2     if(head == null || n = 0)    //head is null condition
 3         return head;
 4     if(n == 1 && head.next == null)    //just having a head condition
 5         return null;
 6     ListNode fast = head;
 7     ListNode lower = head;
 8     for(int i = 0; i < n; i++)
 9         fast = fast.next;
10     if(fast == null){    //removing of the head
11         head = head.next;
12         return head;
13     }
14     while(fast.next != null){
15         fast = fast.next;
16         lower = lower.next;
17     }
18     lower.next = lower.next.next;
19     return head;
20 }
时间: 2024-12-25 20:35:12

Leetcode 19 Remove Nth Node From End of List (快慢指针)的相关文章

leetCode 19. Remove Nth Node From End of List 链表

19. Remove Nth Node From End of List 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 lis

leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法

Remove Nth Node From End of List 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

[LeetCode]#19 Remove Nth Node From the End of list

从移动回来一个月,就一直在忙项目申请书的事情.通过写申请书,看新闻联播的新闻稿都开始顺眼了. 师兄师姐们已经开始找工作了,我还有一年时间,工作方面早作准备.机器学习方面继续推导常见的算法,刷一刷kaggle,做做特征工程什么的,算是应用了. 今天这道题其实并不难,但我是在Linux下写的,有些本地调试的方法和技巧想记录一下. 一.题目 Given a linked list, remove the nth node from the end of list and return its head

LeetCode 19. Remove Nth Node From End of List

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

leetcode 19. Remove Nth Node From End of List(链表)

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

Leetcode 19. Remove Nth Node From End of List JAVA语言

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:

Java [leetcode 19]Remove Nth Node From End of List

题目描述: 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:

LeetCode 19 Remove Nth Node From End of List (C,C++,Java,Python)

Problem: 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. Not

LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点

题目: 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: Gi