BM1 反转链表

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode* t=nullptr;
        ListNode* s=pHead;
        ListNode* n=nullptr;
        while(s){
            n=s->next;
            s->next=t;
            t=s;
            s=n;
        }
        return t;
    }
};

BM2 链表内指定区间反转

/**

 * struct ListNode {
 * int val;
 * struct ListNode *next;
 * };
   */

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
        ListNode* d=new ListNode(0);
        d->next=head;
        ListNode* pre=d;
        ListNode* cur=head;
        for(int i=1;i<m;i++){
            pre=pre->next;
        }
        cur = pre->next;
        for(int i =0;i<(n-m);i++){
            ListNode* temp = cur->next;
            cur->next=cur->next->next;
            temp->next=pre->next;
            pre->next=temp;
        }
        return d->next;
    }
};