Leetcode: Palindrome Linked List (Kotlin)
Leetcode: Palindrome Linked List (Kotlin)
This is an easy Linked List problem on Leetcode. This problem is pretty cool because we can use the last two problems I wrote about to solve this one. I will link the articles later in the article. Palindrome Linked List - LeetCode
Problem Statement
Examples
Constraints
Brainstorm
First, we should define what a palindrome is to better understand the problem. The Oxford dictionary defines it as “a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.”
We can use the two problems we did previously to solve this one. We want to find the middle of the linked list, reverse it, then compare it with the original list up to the middle.
Note that the reversed list is the second half of the list. We will check each value of the reversed list and the first half of the list. If any of the values are not equal, then it is not a palindrome. If all values are the same, we will return true at the end.
Solution
{% gist https://gist.github.com/cmcoffeedev/7ef90e2be8592e0cffb28d9572a2438c.js %} Join Medium with my referral link. Your membership fee directly supports Christopher Coffee and other writers you read. You’ll also get full access to every story on Medium. Join Medium with my referral link - Christopher Coffee