Remove Nth Node From End of List leetcode 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:
Given n will always be valid.
Try to do this in one pass.

题解:

这道题也是经典题,利用的是faster和slower双指针来解决。

首先先让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并returnhead就够了。

代码如下:

1    public static ListNode removeNthFromEnd(ListNode head, int n) {
 2         if(head == null || head.next == null)
 3             return null;
 4             
 5         ListNode faster = head;
 6         ListNode slower = head;
 7         
 8         for(int i = 0; i<n; i++)
 9             faster = faster.next;
10             
11         if(faster == null){
12             head = head.next;
13             return head;
14         }
15         
16         while(faster.next != null){
17             slower = slower.next;
18             faster = faster.next;
19         }
20         
21         slower.next = slower.next.next;
22         return head;
23         
24         }

Remove Nth Node From End of List leetcode java

时间: 2024-12-17 21:31:37

Remove Nth Node From End of List leetcode java的相关文章

[Lintcode]174. Remove Nth Node From End of List/[Leetcode]

174. Remove Nth Node From End of List/19. Remove Nth Node From End of List 本题难度: Easy/Medium Topic: Linked List Description Given a linked list, remove the nth node from the end of list and return its head. Example Example 1: Input: list = 1->2->3-&

Remove Nth Node From End of List leetcode

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 

19. Remove Nth Node From End of List Leetcode Python

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

[Leetcode][Python]19: Remove Nth Node From End of List

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 19: Remove Nth Node From End of Listhttps://oj.leetcode.com/problems/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 ex

LeetCode: Remove Nth Node From End of List [019]

[题目] 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: G

LeetCode 第 19 题 (Remove Nth Node From End of List)

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 li

LeetCode:Remove Nth Node From End of List

1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3.题目内容 英文:Given a linked list, remove the nth node from the end of list and return its head. 中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点 例如:

[leetcode]Remove Nth Node From End of List @ Python

原题地址:http://oj.leetcode.com/problems/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 n

LeetCode: Remove Nth Node From End of List 解题报告

Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Question Solution 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,