You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 Palindrome Linked List 🔥 || 4 Solutions || Simple Fast and Easy || with Explanation
Solution - 1 Two Pointers
classSolution {
boolisPalindrome(ListNode? head) {
// to store the valuesList<int> array = [];
// if there is value we will add into our listif (head !=null) {
array.add(head.val);
// pointing it to the next value
head = head.next;
}
// now all the values is inside the list so let's work on list// - pointer on the lift valueint left =0;
// pointer on the right valueint right = array.length -1;
// assuming that he value on left side is less than the right sidewhile (left < right) {
// reading forward values from left side via ++// reading it backward values via --// from forward to backward are not same than it's not palindromeif (array[left++] != array[right--]) returnfalse;
}
returntrue;
}
}
Solution - 2 Mathematical
classSolution {
// Runtime: 628 ms, faster than 51.02% of Dart online submissions for Palindrome Linked List.// Memory Usage: 194.6 MB, less than 69.39% of Dart online submissions for Palindrome Linked List.boolisPalindrome(ListNode? head) {
// if everything is null than it's true means no value availableif (head ==null|| head.next ==null) returntrue;
// first value on the headint first = head.val;
// second value both needs to compareint second = head.val;
// multiplierint currentMultiplier =10;
// while nothing is null means we have valueswhile ((head = head?.next) !=null) {
// simple multiplication and summation
first = (first *10) + head!.val;
second = second + (currentMultiplier * head.val);
currentMultiplier *=10;
}
// if both are same boom Congratulationreturn first == second;
}
}
Solution - 3 Recursive
classSolution {
// Runtime: 595 ms, faster than 63.27% of Dart online submissions for Palindrome Linked List.// Memory Usage: 194.5 MB, less than 69.39% of Dart online submissions for Palindrome Linked List.ListNodereverseLL(ListNode head) {
ListNode? prev =null, next =null, cur = head;
while (cur !=null) {
// creating duplicate of every node during iterationListNode temp =newListNode(cur.val, cur.next);
// swapping prev & next of 'temp' node
next = temp.next;
temp.next = prev;
prev = temp;
cur = next; // moving ahead in original LL
}
return prev!;
}
boolisPalindrome(ListNode? head) {
ListNode? tail =reverseLL(head!); // got the head of reversed LLwhile (head !=null) {
if (head.val != tail?.val) returnfalse;
head = head.next;
tail = tail?.next;
}
returntrue;
}
}
SOlution - 4 Something Complicated
classSolution {
boolisPalindrome(ListNode? head) {
ListNode? temp = head;
ListNode? midNode =getMiddle(temp!);
ListNode? tail =reverseLL(midNode);
while (tail !=null) {
if (tail.val != head?.val) returnfalse;
tail = tail.next;
head = head?.next;
}
returntrue;
}
ListNodegetMiddle(ListNode head) {
// using tortoise-hare method to find middle elementListNode? prevTort =null, tort = head, hare = head;
while (hare !=null&& hare.next !=null) {
prevTort = tort;
tort = tort?.next;
hare = hare.next?.next;
}
if (tort !=null) // case when LL is of odd lengthreturn tort;
return prevTort!; // case of even length we'll get 2 mid. So, returning 1st mid node.
}
ListNodereverseLL(ListNode head) {
ListNode? prev =null, next =null, cur = head;
while (cur !=null) {
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev!;
}
}