public static ListNode SwapNodes(ListNode head, int k) { int count = 1; ListNode first = head, second = head.next; while (count < k) { second = second.next; count++; } ListNode temp = second; while (second.next != null) { first = first.next; second = second.next; } int t = temp.val; temp.val = first.val; first.val = t; return(head); }
public ListNode(int val = 0, ListNode next = null) { this.val = val; this.next = next; }