LeetCode——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.

原题链接:https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/

题目:给定一个链表,从尾部删除第n个节点并返回新的链表。

思路:使用两个指针,fast 和 slow,他们的距离是n,于是fast到尾的时候,n所在的节点就是需要删除的节点。

	public ListNode removeNthFromEnd(ListNode head, int n) {
		ListNode slow = head, fast = head;
		if (head.next == null)
			return null;
		for (int i = 1; i <= n; i++)
			slow = slow.next;
		if (slow == null) {
			head = head.next;
			return head;
		}
		while (slow.next != null) {
			slow = slow.next;
			fast = fast.next;
		}
		fast.next = fast.next.next;
		return head;
	}
    // Definition for singly-linked list.
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
时间: 2024-10-09 20:35:59

LeetCode——Remove Nth Node From End of List的相关文章

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]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,

[LeetCode]Remove Nth Node From End of List

提米:删除链表从尾部到头部第N的节点. 思路: 两个指针,一个从头开始前移N个节点后,第二个指针开始移动,当第一指针移动到末尾时,第二个指针指向的是从尾部到头部的第N个节点. 注意: 1.N不合法,大于链表长度 2.要删除的是头结点 /***************************************************************************************** Given a linked list, remove the nth node f

[LeetCode] 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] 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: Given

[Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

Given a linked list, remove the n th 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:  Give

LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *removeNthFromEnd(ListNode *head, int n) { 12 struct

[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