Skip to content

Latest commit

 

History

History
48 lines (46 loc) · 1.03 KB

File metadata and controls

48 lines (46 loc) · 1.03 KB

Palindrome Linked List


  • Question:

Given the head of a singly linked list, return true if it is a palindrome.


  • Example:

alt

Input: head = [1,2,2,1]

Output: true


  • Solution:

Code :

class Solution {
   public boolean isPalindrome(ListNode head) {
    ListNode fast = head, slow = head;
    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        slow = slow.next;
    }
    if (fast != null) { // odd nodes: let right half smaller
        slow = slow.next;
    }
    slow = reverse(slow);
    fast = head;
    
    while (slow != null) {
        if (fast.val != slow.val) {
            return false;
        }
        fast = fast.next;
        slow = slow.next;
    }
    return true;
}

public ListNode reverse(ListNode head) {
    ListNode prev = null;
    while (head != null) {
        ListNode next = head.next;
        head.next = prev;
        prev = head;
        head = next;
    }
    return prev;
}
}