Add a method void reverseFirstN(int n)
to our implementation of
the LinkedList
class that reverses the first n
links
in a list. Implement this method by directly rerouting the links. Do not use an iterator. Do not construct any new nodes. Do not copy the data values. Do not add any other methods.
If n is greater than or equal to the size of the list, all elements are reversed. Otherwise, only the first n elements are reversed.
Example: Iflst
is [A, B,C, D], then after calling
lst.reverseFirstN(3)
, it should be [C, B, A, D].
Draft. Make your method work for the case n = 2.