Check if LL is palindrome or not

Linked-List FAQs (Medium) Medium
  • Linked list operations like this are used in many software applications that require dynamic memory management
  • In particular, the concept of checking if a linked list is a palindrome has a fun association with text processing software
  • For example, in apps that check if a string of text is a palindrome (a word, phrase, or sequence that reads the same backwards as forwards, like "madam" or "12321"), a linked list can be utilized to store characters or digits in sequence and then used to validate the palindrome property
  • This type of string validation is practical in areas such as DNA sequencing where bioinformatics software often needs to check palindromic sequences
  • In more general terms, this problem signifies the usefulness and versatility of linked lists in managing and manipulating data

Given the head of a singly linked list representing a positive integer number. Each node of the linked list represents a digit of the number, with the 1st node containing the leftmost digit of the number and so on. Check whether the linked list values form a palindrome or not. Return true if it forms a palindrome, otherwise, return false.


A palindrome is a sequence that reads the same forward and backwards.

Examples:

Input: head -> 3 -> 7 -> 5 -> 7 -> 3

Output: true

Explanation: 37573 is a palindrome.

Input: head -> 1 -> 1 -> 2 -> 1

Output: false

Explanation: 1121 is not a palindrome.

Input: head -> 9 -> 9 -> 9 -> 9

Constraints

  • 1 <= number of nodes in the Linked List <= 105
  • 0 <= ListNode.val <= 9
  • The number represented does not contain any leading zeroes.

Hints

  • A brute-force approach would involve: Storing all the digits in an array (O(n) space). Checking if the array is a palindrome (O(n) time). Drawback: Uses O(n) extra space, violating in-place constraints.
  • To check for a palindrome in O(n) time and O(1) space, we use the slow and fast pointer technique: Find the middle of the list using two pointers (slow and fast). Reverse the second half of the linked list. Compare the first half with the reversed second half.

Company Tags

Intel Seagate Technology Bloomberg Cloudflare Johnson & Johnson Teladoc Health Micron Technology Lyft Activision Blizzard Shopify Freshworks MongoDB Alibaba Oracle DoorDash Boston Consulting Group PayPal Robinhood Morgan Stanley Roche Optum Snowflake Zynga Etsy PwC Google Microsoft Amazon Meta Apple Netflix Adobe