Leetcode 19--Delete The penultimate node of a linked list

Source: Internet
Author: User

1. Topics

Given a linked list, deletes the penultimate node of the linked list and returns the head node of the linked list.

Example:
Given a linked list: 1->2->3->4->5, and n = 2.
When the second-to-last node is deleted, the list becomes 1->2->3->5.

Description
The given n guarantee is valid.

Advanced:
Can you try to use a scan to implement it?

2. Ideas
    • Defines two pointers P1, p2, and just beginning both pointers point to the head node. If we want to delete the last nth node, we let P2 advance N-1 step , at this time the number of nodes behind the P2 is K, then we want to delete the node is located in the back of the P1 of the K node .

    • As shown, suppose we want to delete the 3rd node, P2 forward 2 steps and point to the third node.

    • There are 1 nodes behind the P2, and the node we want to delete is the 1th node at the back of P1-node 2.

    • A special case is to delete the 4th node, the 1th node. At this point, we just need to have the head pointing to the 2nd node.

    • In other cases, after the P2 pointer arrives at the specified position. Let's get the P2 pointer back one position , and then let P1 and P2 move backwards. When the P2 points to the last node, the P1 points to the previous one of the nodes to be deleted , allowing P1 to point to the 2nd node behind the P1 to delete the specified junction.

    • The following is the process of removing the 1th node from the bottom.

    • Of course, don't forget to release the memory of the deleted node .
    • The code is as follows
/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *};             */class Solution {public:listnode* removenthfromend (listnode* head, int n) {if (head = = NULL) {        return head;            } else {int i = 1;                        ListNode *p1 = head, *p2 = head, *temp = NULL;                while (i! = N)//P2 pointer forward n-1 step {i++;            P2 = p2->next;                } if (P2->next = = NULL)//Delete first node Case {temp = head; Head = head->next;                            Head points to 2nd node} else {P2 = p2->next;//p2 pointer previously in 1 steps                    while (P2->next)//P1, p2 pointer synchronously moves backwards {P2 = p2->next;                P1 = p1->next;    } temp = p1->next;            P1->next = p1->next->next; P1 points to the 2nd node behind it, delete (temp);                Release node memory} return head; }        };

For more highlights, please follow "seniusen"!

Leetcode 19--Delete The penultimate node of a linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.