Skip to main content
LeetCode 186, Medium. Topics: Two Pointers, String. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

Problem

Given a character array s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by a single space. Your code must solve the problem in-place, i.e. without allocating extra space.

Examples

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is an English letter (uppercase or lowercase), digit, or space ’ ’
  • There is at least one word in s
  • s does not contain leading or trailing spaces
  • All the words in s are guaranteed to be separated by a single space

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026