leetcode Reverse Linked List II
不说了,贴代码
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n)
{
if(head==NULL)return NULL;
ListNode *p=head;
int i=1;
for(;i<m;i++)
p=p->next;
for(int j=m;j<n;j++)
{
ListNode *q=p;
for(int k=j;k<n;k++)
{
q=q->next;
}
swap(p->val,q->val);
n--;
p=p->next;
}
return head;
}
};
浙公网安备 33010602011771号