# 解题思维一 （交换两个节点val值）

## 代码如下

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapNodes(ListNode head, int k) {
for(int i = 0;i < k;i++){
fast = fast.next;
if(fast == null){
}
}
ListNode fastCur = fast;
while(fastCur!=null){
fastCur =fastCur.next;
slow = slow.next;
}
int tmp = fast.val;
fast.val = slow.val;
slow.val = tmp;
}
}
``````

# 解题思维二（交换两个节点的位置）

## 代码如下：

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapNodes(ListNode head, int k) {
ListNode fastPrev = null;
ListNode slowPrev = null;
for(int i = 0;i < k;i++){
fastPrev = fast;
fast = fast.next;
if(fast == null){
}
}
ListNode fastCur = fast;
while(fastCur!=null){
fastCur =fastCur.next;
slowPrev = slow;
slow = slow.next;
}
ListNode fastNext = fast.next;
ListNode slowNext = slow.next;
if(fastNext == slow){
fast.next = slowNext;
slow.next = fast;
fastPrev.next = slow;
}else if(slowNext == fast){
slow.next =fastNext;
fast.next = slow;
slowPrev.next =fast;
}else{
slow.next = fastNext;
fastPrev.next = slow;

fast.next = slowNext;
slowPrev.next = fast;
}
}
}
``````

THE END