As reported: Akamai Senior Software Engineer interview (2025), round 1 (coding on HackerRank): reverse the words of a sentence while retaining the punctuation at the end; then delete a node in a linked list given its value.
Given a sentence s, reverse the order of its words, but keep the punctuation at the end of the sentence where it is.
Example 1
s = "Hello world how are you?"
Output: "you are how world Hello?"
Example 2
s = "The quick brown fox!!"
Output: "fox brown quick The!!"
Example 3
s = " keep calm "
Output: "calm keep"
Clarifications
- The end punctuation is the run of characters from
. , ! ? ; :at the very end of the sentence (after trailing spaces are ignored). It is written back, unchanged, right after the last word of the output. - Words are separated by one or more spaces. The output joins the words with single spaces and has no leading or trailing spaces.
- Punctuation inside the sentence stays attached to its word (
"Yes, I can."→"can I Yes,."). - If the sentence has no words, return the end punctuation alone (or
"").
Constraints: 0 ≤ s.length ≤ 10^4; s contains English letters, digits, spaces and . , ! ? ; :.
Code runner format: one JSON line: s. Implement reverseWords(s).