
g0201_0300.s0234_palindrome_linked_list.Solution.cs 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
namespace LeetCodeNet.G0201_0300.S0234_palindrome_linked_list {
// #Easy #Top_100_Liked_Questions #Top_Interview_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_01_10_Time_306_ms_(31.81%)_Space_66.1_MB_(33.64%)
using LeetCodeNet.Com_github_leetcode;
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public bool IsPalindrome(ListNode head) {
int len = 0;
ListNode right = head;
// Calculate the length
while (right != null) {
right = right.next;
len++;
}
// Reverse the right half of the list
len = len / 2;
right = head;
for (int i = 0; i < len; i++) {
right = right.next;
}
ListNode prev = null;
while (right != null) {
ListNode next = right.next;
right.next = prev;
prev = right;
right = next;
}
// Compare left half and right half
for (int i = 0; i < len; i++) {
if (prev != null && head.val == prev.val) {
head = head.next;
prev = prev.next;
} else {
return false;
}
}
return true;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy