1
+ def palindrome (head ):
2
+ """
3
+ Determines if a singly linked list is a palindrome.
4
+
5
+ This function checks if the linked list is a palindrome by using a two-pointer technique to find the middle of the list,
6
+ reversing the second half of the list, and then comparing the two halves.
7
+
8
+ Args:
9
+ head: The head node of the singly linked list.
10
+
11
+ Returns:
12
+ True if the linked list is a palindrome, False otherwise.
13
+ """
14
+ if head is None :
15
+ return True # An empty list is considered a palindrome
16
+
17
+ slow = head # Initialize the slow pointer to the head of the list
18
+ fast = head # Initialize the fast pointer to the head of the list
19
+
20
+ # Find the middle of the linked list
21
+ while fast and fast .next :
22
+ slow = slow .next
23
+ fast = fast .next .next
24
+
25
+ # Reverse the second half of the list
26
+ reversed_half_head = reverse_list (slow )
27
+
28
+ # Compare the first half and the reversed second half
29
+ is_identical = compare_halfs (head , reversed_half_head )
30
+
31
+ # retain linked list in the original order
32
+ reverse_list (reversed_half_head )
33
+
34
+ # Return the result of the comparison
35
+ return is_identical
36
+
37
+ def reverse_list (head ):
38
+ """
39
+ Reverses a singly linked list.
40
+
41
+ Args:
42
+ head: The head node of the list to be reversed.
43
+
44
+ Returns:
45
+ The new head of the reversed list.
46
+ """
47
+ prev = None
48
+ current = head
49
+
50
+ while current :
51
+ next_node = current .next # Store the next node
52
+ current .next = prev # Reverse the current node's pointer
53
+ prev = current # Move prev to the current node
54
+ current = next_node # Move to the next node
55
+
56
+ return prev
57
+
58
+ def compare_halfs (head1 , head2 ):
59
+ """
60
+ Compares two halves of a linked list to check for equality.
61
+
62
+ Args:
63
+ head1: The head of the first half of the list.
64
+ head2: The head of the second half of the list.
65
+
66
+ Returns:
67
+ True if the two halves are identical, False otherwise.
68
+ """
69
+ while head1 and head2 :
70
+ if head1 .val != head2 .val :
71
+ return False # Return False if any values differ
72
+ head1 = head1 .next
73
+ head2 = head2 .next
74
+ return True # Return True if all values are identical
75
+
76
+ # Time Complexity: O(n)
77
+ # The algorithm runs in linear time because it involves three main steps: finding the middle of the list,
78
+ # reversing the second half, and comparing the two halves. Each of these steps requires a single pass through the list.
79
+
80
+ # Space Complexity: O(1)
81
+ # The algorithm uses a constant amount of extra space. It only uses a few pointer variables and does not require
82
+ # any additional data structures that depend on the size of the input list.
83
+
84
+ # Approach:
85
+ # 1. Use the two-pointer technique to find the middle of the linked list.
86
+ # 2. Reverse the second half of the list.
87
+ # 3. Compare the first half and the reversed second half to check for palindrome properties.
88
+ # This approach efficiently determines if the list is a palindrome without using extra space for storing values.
0 commit comments