Concise Java: 1 LinkedList Makes It


  • 0

    We can conveniently poll LinkedList from the head or tail.
    Concatenate the head and tail, and then put the new entry back to the list.

        public String findContestMatch(int n) {
            LinkedList<String> list = new LinkedList<>(); 
            for (int i = 1; i <= n; i++) list.add(String.valueOf(i));
            int size = n; 
            while (size > 1) {
                size /= 2; 
                for (int i = 0; i < size; i++) 
                    list.set(i, "(" + list.get(i) + "," + list.pollLast() + ")"); 
            }
            return list.poll(); 
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.