92:reverse Linked List II flipping linked list "linked list"

Source: Internet
Author: User

Topic Connection: click~

/* Test Instructions: Flips the list of points m through n *//** * idea: To better handle the header and the M nodes, introduce the root node and record the *      m-1 node. Starting from the first node of M to the nth node, insert the already      traversed node after the M-1 node and ensure that the next * of the M node points to the next of the      traversal node to avoid a broken list */class solution {public:    ListNode *reversebetween (ListNode *head, int m, int n) {        ListNode *root = new ListNode (0);        Root->next = head;        ListNode *mnode = new ListNode (0);        ListNode *pre = root, *curr = head;        for (int i = 1; I <= n; i + +) {            if (i = = m) Mnode = Curr;  Record the M node            if (I < m) pre = Curr;     Record the first m-1 node            listnode *next = curr->next;            if (i > M && i <= N) {                mnode->next = next;   Avoid broken list                Curr->next = pre->next;                Pre->next = Curr;            }            Curr = next;        }        Return root->next;    }};

  

92:reverse Linked List II flipping linked list "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.