/** * struct ListNode { * int val; * struct ListNode *next; * }; */ #include <cstddef> typedef struct ListNode LNode; class Solution { public: /** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ ListNode* reverseBetween(ListNode* head, int m, int n) { // write code here LNode *newHead = (LNode *) malloc (sizeof(LNode)); newHead->next = head; LNode *thisNode = head; LNode *priorNode = newHead; //保证有两个以上的节点 LNode *cutNode = newHead; //找到m for(int i = 1; i < m; i++) { cutNode = cutNode->next; } thisNode = cutNode->next; for(int i = m; i <= n; i++) { LNode *tempNode = thisNode->next; thisNode->next = priorNode; priorNode = thisNode; thisNode = tempNode; } cutNode->next->next = thisNode; cutNode->next = priorNode; return newHead->next; } };