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"