【leetcode】Remove Nth Node From End of List(easy)

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.

思路:

最基本的思路肯定是用两个指针,一个先走n+1步,然后再两个同时走,这样后走的指针一定是在要删除指针的前一格。

问题主要是如果要删除的是头结点,走到n步的时候肯定就NULL了,所以要加个判断,如果在没有走完n+1步时遇到了 先走指针指向NULL,则返回head->next

唉,就这一点一点逻辑关系晕了好久。要先分析清楚再做题,不要每次都只是靠感觉...感觉在这种边界条件时特别的不靠谱...

ListNode *removeNthFromEnd(ListNode *head, int n) {
        ListNode * p1 = head, * p2 = head;
        n++;
        while(n--) //p1先走n+1步
        {
            p1 = p1->next;
            if(p1 == NULL && n != 0) //遇到删除头指针情况
                return head->next;
        }
        while(p1 != NULL) //p1走到头时, p2正好在要删除的指针前面
        {
            p1 = p1->next;
            p2 = p2->next;
        }
        p2->next = p2->next->next; //删除指定指针
        return head;
    }
时间: 2024-10-25 13:28:44

【leetcode】Remove Nth Node From End of List(easy)的相关文章

【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】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 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 之Remove Nth Node From End of List(19)

这题比较简单,方法有很多.其中一种比较有意思的做法是设置两个指针,一个先走n步,然后再一起走.一个到了末尾,另一个也就确定了要删除元素的位置. ListNode *removeNthFromEnd(ListNode *head, int n) { ListNode dummy{-1, head}; ListNode *p = &dummy, *q = &dummy; for (int i = 0; i < n; i++) // q 先走 n 步 q = q->next; whi

Leetcode 19. Remove Nth Node From End of List(python)

链表操作,只能遍历一遍然后. 用双指针 # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int

leetcode_19题——Remove Nth Node From End of List(链表)

Remove Nth Node From End of List Total Accepted: 54129 Total Submissions: 197759My 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,

19. Remove Nth Node From End of List(js)

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

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

19. Remove Nth Node From End of List(medium)

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. 思路:遍历一次若链表长度