1class Solution {2public ListNode swapNodes(ListNode head, int k) {3ListNode fast = head;4ListNode slow = head;5ListNode first = head, second = head;67// Put fast (k-1) nodes after slow8for (int i = 0; i < k - 1; ++i) fast = fast.next;910// Save the node for swapping11first = fast;1213// Move until the end of the list14while (fast.next != null) {15slow = slow.next;16fast = fast.next;17}1819// Save the second node for swapping20// Note that the pointer second isn't necessary: we could use slow for swapping as well21// However, having second improves readability22second = slow;2324// Swap values25int temp = first.val;26first.val = second.val;27second.val = temp;2829return head;30}31}