Loading...
Loading...
For the college fest, a corridor is decorated with a chain of coloured lights. Each light has a colour code, and the chain is stored as a linked list. The chain looks right only if it reads the same from the first light to the last and from the last to the first. Print YES if it does and NO if it does not. Try to use only a few extra variables, not a copy of the list.
YES if the chain reads the same in both directions, otherwise NO.Example 1
Input
5 3 7 9 7 3
Output
YES
Read from either end, the chain is 3 7 9 7 3.
Example 2
Input
4 1 2 2 3
Output
NO
The first light is 1 but the last is 3.
Try the problem first. Open one hint at a time.
Find the middle of the list with a slow reference and a fast reference.
Turn the second half around so it runs from the last light back to the middle.
Now walk the first half from the head and the turned half from its new head at the same time, comparing values.
For an odd length, the middle light has nothing to match, so it is fine to leave it out of the comparison.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor