Written question 32. Leetcode OJ (19)

Source: Internet
Author: User

Finally ran into a data structure related to the topic, which also means that the complexity of the increase. To take a look at this problem, to delete the bottom of the list of K nodes, we need to find out the length of the list and then determine the penultimate K node? I don't think that's necessary. Because our method is more skillful than this, it sounds tall and still. The idea of solving problems and the following considerations:

1. I am using two pointers, one fast and one slow, go first k+1 step, then quick pointer and slow pointer go together, until the quick pointer to the tail

2. Why do we need a quick hand to walk k+1 step? Let's think about it, we're using a slow pointer to decide which node to delete, if the slow pointer just means the node to be deleted, then congratulations, you need to go through the list again, find the previous node of the slow pointer, and then delete the slow pointer, the last link linked list.

3. So fast pointer fast Walk k+1 step is reasonable, this kind of situation needs to consider a special case, that is to delete the head node situation, my solution is to use the fast pointer termination position to judge, namely:

int M=k+1;while (M && fast) {fast = Fast->next;--m;}
finally determine the value of M:

(1). If m>1, then the link table length is not enough, direct exit

(2). If m==1, this means that the node to be deleted is the head node, when the head nodes point to the next node and then return

(3). Other things are simple, and the slow pointer points to the previous node of the node you want to delete, so we can handle it.

So the entire code looks like this:

/** * 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) {//delete the penultimate node of the linked list, the idea is a quick and slow pointer, and the fast pointer goes first n steps        And then slow down together if (head = = NULL | | n < 0) {return null;        } ListNode *fast=head;        ListNode *slow=head; int m=n+1;            Returns the previous position of the deleted position while (fast && m) {fast=fast->next;        --m;        if (M >1) {//The length of the list is not enough N, why is the control condition greater than 1, because I am looking for the previous node of the deleted node return NULL;            } while (fast) {fast=fast->next;        slow=slow->next;            } if (M = = 1) {//deleted node is head node ' heads = head->next;        Delete slow;            } else {ListNode *tobedel = slow->next;            slow->next=slow->next->next;        Delete Tobedel;} return head; }};
Finally, I'm just attaching this.




Written question 32. Leetcode OJ (19)

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.