Swap Kth From Start and End
Swap the values of the k-th node from the beginning (k = 1 is the head) and the k-th node from the end (k = 1 is the last node). Return the head. If both are the same node, nothing changes. The list is passed in as head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
head [1,2,3,4,5]
k 2
returns [1,4,3,2,5]
The 2nd from the start is 2, the 2nd from the end is 4.
Example 2
head [7,9,6,6,7,8,3,0,9,5]
k 5
returns [7,9,6,6,8,7,3,0,9,5]
The 5th from each side are 7 and 8.
Constraints
1 <= k <= number of nodes <= 100-100 <= Node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.