Leetcode first _ Submission Details

Source: Internet
Author: User

Some time has not been updated. I spent a few days paying attention to it. After a dozen times, I decided to finish the first season, and then analyzed it again according to my understanding, the second time should be classified by problem. The first season should be referenced. If you think of or find a better solution, the first season will be updated.

The problem with linked lists is disgusting. Most of the ideas are direct and you should be very careful with pointer operations. I often make the following mistakes:

1. When val or next is obtained, no blank judgment is given, resulting in a Runtime Error.

2. Empty pointers and boundary conditions such as only one node.

3. after the linked list is updated, the tail of the linked list is not empty, causing it to point to another node to form a ring, or the tail of the part of the linked list is not empty to form a ring.

4. When multiple pointers are used at the same time, translation is performed. When the pointer is updated for use in the next loop, the update sequence of the pointer is incorrect, resulting in a null pointer or loop, or a TLE or RE occurs.

This question is also interesting. The first node is followed by the last one, and the second is followed by the second one. I first read the wrong question and thought it was from half, the first line is n/2. But the idea is almost the same. To solve this problem, you only need to find half of the position and reverse the Chain List behind it. Then, you can use a head pointer and a pointer at half position to update at the same time.

The O (N) and fixed control inversion of the linked list are tools to solve many linked list problems. They are easy to write errors and should be written several times. I have a good understanding of the writing, using three pointers.

The following is the ac code:

ListNode *reverse(ListNode *head){    if(!head || !head->next)        return head;    ListNode *one = head, *two = head->next, *three = head->next->next;    one->next = NULL;    while(three){        two->next = one;        one = two;        two = three;        three = three->next;    }    two->next = one;    return two;}class Solution {public:    void reorderList(ListNode *head) {        if(!head || !head->next)            return;        int len=0;        ListNode *pNode = head, *rpNode = head, *pre = NULL;        while(pNode){            len++;            pNode = pNode->next;        }        if(len == 2)            return;        len = (len+1)/2;        for(int i=0;i
  next;        }        if(pre)            pre->next = NULL;        rpNode = reverse(rpNode);        ListNode *test = rpNode;        pNode = head;        while(rpNode){            pre = rpNode->next;            rpNode->next = pNode->next;            pNode->next = rpNode;            pNode = pNode->next->next;            rpNode = pre;        }        return;    }};

Related Article

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.