Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Add Linked List Palindrome check #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions Python/LinkedListPalindromeCheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
"""
Problem - Check if a linked list is a palidrome or not

Algorithm Explaination:
1. Get the middle of the linked list.
2. Reverse the second half of the linked list.
3. Check if the first half and second half are identical.
4. Construct the original linked list by reversing the second half again and attaching it back to the first half

Time Complexity - O(n)
Auxiliary Space Complexity - O(1)


Example Test Cases -
1. I/P - ['a', 'b', 'a', 'c', 'a', 'b', 'a']
O/P - "Is Palindrome"
2. I/P - ['a']
O/P - "Is Palindrome"
3. I/P - ['a', 'b', 'c']
O/P - "Not Palindrome"
"""

class Node:
def __init__(self, data):

self.data = data
self.next = None


class LinkedList:
def __init__(self):
self.head = None

def isPalindrome(self, head):
slow_ptr = head
fast_ptr = head
prev_of_slow_ptr = head

midnode = None

res = True

if (head != None and head.next != None):
while (fast_ptr != None and
fast_ptr.next != None):

fast_ptr = fast_ptr.next.next
prev_of_slow_ptr = slow_ptr
slow_ptr = slow_ptr.next

if (fast_ptr != None):
midnode = slow_ptr
slow_ptr = slow_ptr.next

second_half = slow_ptr

prev_of_slow_ptr.next = None

second_half = self.reverse(second_half)

res = self.compareLists(head, second_half)

second_half = self.reverse(second_half)

if (midnode != None):
prev_of_slow_ptr.next = midnode
midnode.next = second_half
else:
prev_of_slow_ptr.next = second_half
return res

def reverse(self, second_half):

prev = None
current = second_half
next = None

while current != None:
next = current.next
current.next = prev
prev = current
current = next

second_half = prev
return second_half

def compareLists(self, head1, head2):

temp1 = head1
temp2 = head2

while (temp1 and temp2):
if (temp1.data == temp2.data):
temp1 = temp1.next
temp2 = temp2.next
else:
return 0

if (temp1 == None and temp2 == None):
return 1

return 0

def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node

def printList(self):

temp = self.head

while(temp):
print(temp.data, end="->")
temp = temp.next

print("NULL")


# Driver code
if __name__ == '__main__':

ll = LinkedList()
s = ['a','b','c']

for i in range(len(s)):
ll.push(s[i])
ll.printList()

if (ll.isPalindrome(ll.head) != False):
print("Is Palindrome\n")
else:
print("Not Palindrome\n")
print()
82 changes: 42 additions & 40 deletions Python/README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
Add links to your code in Alphabetical Order.
Format: -[Program name](name of the file)

[Binary Search](binarySearch.py)

[Bubble Sort](bubbleSort.py)

[Dijkstra's Algorithm](Dijkstra's.py)

[Union Find Data Structure](ufds.py)

[Kadane's Algorithm to find Largest Sum contiguous subarray](kadanes_algorithm_largest_sum_contiguous_subarray.py)

[Linear Search](LinearSearch.py)

[Longest Substring Without Repeating Characters](lengthOfLongestSubstring.py)

[Merge Sort](MergeSort.py)

[Palindrome Check](palindrome_check.py)

[Pascal's Triangle](Pascles_Triangle.py)

[Row_with_minimum_number_of 1's](Row_with_minimum_number_of_1's.py)

[Selection Sort](selectionSort.py)

[Shortest Path in Unweighted Graph](shortest_path_unweighted.py)

[Tower of Hanoi(Recusion)](towerOfHanoi.py)

[Row_with_minimum_number_of 1's](Row_with_minimum_number_of_1's.py)

[Floyd Warshall](Floyd.py)

[Maximum Sum Increasing Subsequence](MaximumSumIncreasingSubsequence.py)

[01Knapsack](01Knapsack.py)

[Insertion Sort](Insertion_Sort.py)
Add links to your code in Alphabetical Order.
Format: -[Program name](name of the file)

[Binary Search](binarySearch.py)

[Bubble Sort](bubbleSort.py)

[Dijkstra's Algorithm](Dijkstra's.py)

[Union Find Data Structure](ufds.py)

[Kadane's Algorithm to find Largest Sum contiguous subarray](kadanes_algorithm_largest_sum_contiguous_subarray.py)

[Linear Search](LinearSearch.py)

[Linked List Palindrome](LinkedListPalindromeCheck.py)

[Longest Substring Without Repeating Characters](lengthOfLongestSubstring.py)

[Merge Sort](MergeSort.py)

[Palindrome Check](palindrome_check.py)

[Pascal's Triangle](Pascles_Triangle.py)

[Row_with_minimum_number_of 1's](Row_with_minimum_number_of_1's.py)

[Selection Sort](selectionSort.py)

[Shortest Path in Unweighted Graph](shortest_path_unweighted.py)

[Tower of Hanoi(Recusion)](towerOfHanoi.py)

[Row_with_minimum_number_of 1's](Row_with_minimum_number_of_1's.py)

[Floyd Warshall](Floyd.py)

[Maximum Sum Increasing Subsequence](MaximumSumIncreasingSubsequence.py)

[01Knapsack](01Knapsack.py)

[Insertion Sort](Insertion_Sort.py)