一、题目
二、思路
1、采用双指针方式找到两个节点
2、交换两个节点的值
三、代码
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
ListNode *fast=head;
ListNode *cur=head;
for(int i=0;i<k-1;++i){
cur=cur->next;
}
ListNode *first=cur;
while(cur->next){
fast=fast->next;
cur=cur->next;
}
int tmp=first->val;
first->val=fast->val;
fast->val=tmp;
return head;
}
};