关于
lintcode系列,第36题,题目网址:https://www.lintcode.com/problem/reverse-linked-list-ii/description
描述
翻转链表中第m个节点到第n个节点的部分,m,n满足1 ≤ m ≤ n ≤ 链表长度。
样例:
例1:
输入: 1->2->3->4->5->NULL, m = 2 and n = 4,
输出: 1->4->3->2->5->NULL.
例2:
输入: 1->2->3->4->NULL, m = 2 and n = 3,
输出: 1->3->2->4->NULL.
思路
实现得比较繁琐,指针p用来遍历,pre和pree用来记录需要翻转的节点,分情况讨论。
C++实现
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: ListNode head is the head of the linked list
* @param m: An integer
* @param n: An integer
* @return: The head of the reversed ListNode
*/
ListNode *</