[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


019.Remove_Nth_Node_From_End_of_List (Easy)

链接

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

代码(github):https://github.com/illuz/leetcode

题意

删除一个单向链表的倒数第 N 个节点。

分析

  1. 直接模拟,先算出节点数,再找到节点删除
  2. 用两个指针,一个先走 N 步,然后再一起走。

这里用 C++ 实现第一种, 用 Python 实现第二种。

Java 的话和 C++/Python 差不多,不写出来了。

代码

C++:

class Solution {
public:
    ListNode *removeNthFromEnd(ListNode *head, int n) {
		if (n == 0)
			return head;
		// count the node number
		int num = 0;
		ListNode *cur = head;
		while (cur != NULL) {
			cur = cur->next;
			num++;
		}
		if (num == n) {
			// remove first node
			ListNode *ret = head->next;
			delete head;
			return ret;
		} else {
			// remove (cnt-n)th node
			int m = num - n - 1;
			cur = head;
			while (m--)
				cur = cur->next;
			ListNode *rem = cur->next;
			cur->next = cur->next->next;
			delete rem;
			return head;
		}
    }
};

Python:

class Solution:
    # @return a ListNode
    def removeNthFromEnd(self, head, n):
        dummy = ListNode(0)
        dummy.next = head
        p, q = dummy, dummy

        # first 'q' go n step
        for i in range(n):
            q = q.next

        # q & p
        while q.next:
            p = p.next
            q = q.next

        rec = p.next
        p.next = rec.next
        del rec
        return dummy.next
时间: 2024-10-13 10:39:25

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)的相关文章

LeetCode 019 Remove Nth Node From End of List

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

Java for LeetCode 019 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

[Leetcode][019] Remove Nth Node From End of List (Java)

题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointers [个人分析] 这个题目应该算是Linked List里面的基础题.说它基础不是因为它简单,而是因为它是基石.一些 Linked list中经典方法在这道题里面都有应用. 1. Two Pointers 在 Linked List中: 如果能预先链表知道长度,那题目就简单多了,删掉从头开始的 Le

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 OJ - 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:Give

[LeetCode][JavaScript]Remove Nth Node From End of List

https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 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

【LeetCode】Remove Nth Node From End of List (2 solutions)

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