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;
}
};
- 本文链接:https://archer-lan.github.io/2022/03/12/%E7%89%9B%E5%AE%A2%E5%88%B7%E9%A2%98-DAY1/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。