
g0201_0300.s0234_palindrome_linked_list.Solution.dart Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Easy #Top_100_Liked_Questions #Two_Pointers #Stack #Linked_List #Recursion
// #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
// #2024_10_09_Time_365_ms_(87.93%)_Space_203.1_MB_(36.21%)
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode? next;
* ListNode([this.val = 0, this.next]);
* }
*/
class Solution {
bool isPalindrome(ListNode? head) {
List list = [];
while (head != null) {
list.add(head.val);
head = head.next;
}
int l = 0;
int r = list.length - 1;
while (l < list.length && r >= 0 && list[l] == list[r]) {
l++;
r--;
}
return l == list.length;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy