Implement a LinkedList class that represents a singly linked list of integers.
The LinkedList class should support the following operations:
LinkedList() — Initialize an empty linked list.addFirst(val) — Insert the integer val at the beginning of the list.addLast(val) — Insert the integer val at the end of the list.removeFirst() — Remove and return the first element. Return -1 if the list is empty.contains(val) — Return true if val exists in the list, false otherwise.toArray() — Return an array of all elements in the list from head to tail.size() — Return the number of elements in the list.["LinkedList","addFirst","addFirst","addLast","toArray","removeFirst","contains","size"]
[[],[2],[1],[3],[],[],[2],[]][null,null,null,null,[1,2,3],1,true,2]-1000 <= val <= 1000At most 1000 calls will be made to addFirst, addLast, removeFirst, contains, toArray, and sizeremoveFirst returns -1 when the list is emptyRun your code to see results
Use Cmd+Enter to run