Compress Runs
Replace every run of equal neighboring values with two nodes: first the length of the run, then the value. For 5 -> 5 -> 5 -> 2 -> 2 -> 5 the answer is 3 -> 5 -> 2 -> 2 -> 1 -> 5. Return the head. 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 [5,5,5,2,2,5]
returns [3,5,2,2,1,5]
Three 5s, two 2s, one 5.
Example 2
head [1,2,3]
returns [1,1,1,2,1,3]
Three runs of length one.
Constraints
0 <= number of nodes <= 50-100 <= Node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.