This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu
Using Stack
/**
* @param {string} path
* @return {string}
*/
var simplifyPath = function (path) {
let myStack = [];
for (char of path.split("/")) {
if (char == "" || char == ".") {
continue
}
if (char == "..") {
if (myStack.length > 0) {
myStack.pop(); // Only pop if there's something to pop
}
} else {
myStack.push(char)
}
}
return "/" + myStack.join("/")
};
This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.

APA
MLA
Rakesh Reddy Peddamallu | Sciencx (2025-03-21T08:33:28+00:00) Leetcode – 71. Simplify Path. Retrieved from https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/
" » Leetcode – 71. Simplify Path." Rakesh Reddy Peddamallu | Sciencx - Friday March 21, 2025, https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/
HARVARDRakesh Reddy Peddamallu | Sciencx Friday March 21, 2025 » Leetcode – 71. Simplify Path., viewed ,<https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/>
VANCOUVERRakesh Reddy Peddamallu | Sciencx - » Leetcode – 71. Simplify Path. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/
CHICAGO" » Leetcode – 71. Simplify Path." Rakesh Reddy Peddamallu | Sciencx - Accessed . https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/
IEEE" » Leetcode – 71. Simplify Path." Rakesh Reddy Peddamallu | Sciencx [Online]. Available: https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/. [Accessed: ]
rf:citation » Leetcode – 71. Simplify Path | Rakesh Reddy Peddamallu | Sciencx | https://www.scien.cx/2025/03/21/leetcode-71-simplify-path/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.