1721.cpp (384B)
1 class Solution { 2 public: 3 ListNode *swapNodes(ListNode *head, int k) { 4 ListNode *tmp = head, *first; 5 while (--k) 6 tmp = tmp->next; 7 first = tmp; 8 9 ListNode *second = head; 10 tmp = tmp->next; 11 while (tmp) 12 tmp = tmp->next, second = second->next; 13 swap(first->val, second->val); 14 return head; 15 } 16 };