From cf57559f0daf40b08be913fe1acf02286cd8d0c6 Mon Sep 17 00:00:00 2001 From: TKushireddy <136903229+TKushireddy@users.noreply.github.com> Date: Mon, 4 Dec 2023 22:02:30 +0530 Subject: [PATCH] Add files via upload --- .../Easy/LinkedListPalindrome/CodeBlogs.md | 265 ++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 LinkedList/Easy/LinkedListPalindrome/CodeBlogs.md diff --git a/LinkedList/Easy/LinkedListPalindrome/CodeBlogs.md b/LinkedList/Easy/LinkedListPalindrome/CodeBlogs.md new file mode 100644 index 00000000..b5adf3e3 --- /dev/null +++ b/LinkedList/Easy/LinkedListPalindrome/CodeBlogs.md @@ -0,0 +1,265 @@ +# Linked List Palindrome + +## Introduction to Linked List Palindrome + +A Linked List Palindrome refers to a linked list where the sequence of values in the nodes is the same forwards as it is backwards. This concept is often explored in computer science and data structures. It provides an interesting challenge for algorithm design and can be used to understand and practice various techniques for traversing and analyzing linked lists. + +## Overview of Linked List Palindrome + +A linked list is a data structure in which elements point to the next element in the sequence, forming a chain-like structure. A palindrome is a sequence that reads the same backwards as forwards. Therefore, a linked list palindrome is a linked list that reads the same forwards and backwards. This is usually achieved when the sequence of node values in the linked list is the same when read from the first node to the last, and vice versa. This concept, while simple in theory, can be complex in implementation, and is a common problem presented in computer science and data structures courses. + +## Code + +```python +# Copy rights to venkys.io +# For more information visit https://venkys.io +# Python program to perform Linked List Palindrome +# Stable:Yes +# Inplace:Yes +# Adaptive:No +# Space complexity: O(1) +# Time complexity:O(n) +# This class represents a node in a linked list. It has two attributes: data and next. +# The data attribute stores the value of the node, and the next attribute points to the next node in the list. +class Node: + def __init__(self,data): + self.data=data + self.next=None +# This function takes a list as input and creates a linked list from it. +# It iterates over the elements of the list and creates a new node for each element. +def createlist(llist): + # If the head of the linked list is None, it sets the head to the newly created node. + # Otherwise, it appends the new node to the end of the linked list. + head=None + temp=None + for i in llist: + if not temp: + temp=Node(i) + head=temp + else: + temp.next=Node(i) + temp=temp.next + return head + +def checkpalindrome(head): + temp=head + stack=[] # It uses a stack to store the values of the linked list in reverse order. + while head: + stack.append(head.data) + head=head.next + + while temp: + if temp.data!=stack.pop(): # Then, it compares the values of the linked list with the values popped from the stack. + return False # If any value doesn't match, it returns False. + temp=temp.next + return True # Otherwise, it returns True. + +if __name__=="__main__": + llist=[10,20,30,20,10] + head=createlist(llist) + print(checkpalindrome(head)) +``` + +## Step-by-Step Explanation + +1. **Node Class Definition**: The `Node` class is defined with an initializer that takes a data parameter and sets the `next` attribute to `None`. This class will be used to create nodes for the linked list. +2. **Creating the Linked List**: The `createlist` function takes a list as input and creates a linked list from it. It initializes the `head` and `temp` to `None`. Then, for each element in the input list, it creates a new node and updates `temp` to the new node. +3. **Checking for Palindrome**: The `checkpalindrome` function takes the head of the linked list as input and checks if the linked list is a palindrome. It initializes an empty stack and a `temp` variable pointing to the head. Then, it iterates through the linked list, adding each node's data to the stack. Next, it iterates through the linked list again, this time popping elements from the stack and comparing them with the node data. If a node's data does not match the popped stack element, it returns `False`. If it completes the iteration without finding a mismatch, it returns `True`. +4. **Main Function**: In the main function, a list is defined, and the `createlist` function is called with this list to create a linked list. The `checkpalindrome` function is then called with the head of this linked list, and the result is printed. If the linked list is a palindrome, it prints `True`; otherwise, `False`. + +## Code + +```java +//Copy rights to venkys.io +//For more information visit https://venkys.io +// Java program to perform Linked List Palindrome +// Stable:Yes +// Inplace:Yes +// Adaptive:No +// Space complexity: O(1) +// Time complexity:O(n) +import java.util.Stack; +class Node{ + int data; + Node next; + Node(int data){ + this.data=data; + } +} + +public class Main{ + // Create a linked list from an array of integers + static Node createList(int[] llist){ + Node head=null,temp=null;// If the head of the linked list is None, it sets the head to the newly created node + for(int ele:llist) + { + if(temp==null) + { + temp=new Node(ele);//it sets the head to the newly created node + head=temp; + } + else{ + temp.next=new Node(ele); + temp=temp.next; // it appends the new node to the end of the linked list + } + } + return head; + } + // Check if a linked list is a palindrome + static boolean chechPalindrome(Node head){ + Node temp=head; + Stack stack = new Stack<>(); // It uses a stack to store the values of the linked list in reverse order + while(head!=null){ + stack.push(head.data); + head=head.next; + } + while(temp!=null){ + if(temp.data!=stack.pop()) return false; // If any value doesn't match, it returns False. + temp=temp.next; + } + return true; // else it returns true + } + public static void main(String[] args) { + int[] llist = {10,20,30,20,10}; + Node head=createList(llist); + System.out.println(chechPalindrome(head)); + + } +} +``` +## Step-by-Step Explanation + +1. **Node Class Definition**: The `Node` class is defined with an initializer that accepts a data parameter. This class is used to create nodes for the linked list. +2. **Creating the Linked List**: The `createList` function takes an array as input and creates a linked list from it. It initializes `head` and `temp` to `null`. Then, for each element in the input array, it creates a new node and updates `temp` to the new node. +3. **Checking for Palindrome**: The `chechPalindrome` function takes the head of the linked list as input and checks if the linked list is a palindrome. It initializes an empty stack and a `temp` variable pointing to the head. Then, it goes through the linked list, adding each node's data to the stack. Next, it goes through the linked list again, this time popping elements from the stack and comparing them with the node data. If a node's data does not match the popped stack element, it returns `false`. If it completes the iteration without finding a mismatch, it returns `true`. +4. **Main Function**: In the main function, an array is defined, and the `createList` function is called with this array to create a linked list. The `chechPalindrome` function is then called with the head of this linked list, and the result is printed. If the linked list is a palindrome, it prints `true`; otherwise, it prints `false`. + +## Code + +```cpp +/* Copy rights to venkys.io +For more information visit https://venkys.io*/ +// CPP program to perform Linked List Palindrome +// Stable:Yes +// Inplace:Yes +// Adaptive:No +// Space complexity: O(1) +// Time complexity:O(n) +#include +using namespace std; /* Create a linked list from an array of integers */ +class VSDnode{ + + public: + int data; + VSDnode* next; + + VSDnode(int val){ + data=val; + next=NULL; +} + +}; +void add_head(VSDnode* &head,int val){ + VSDnode* n= new VSDnode(val); + n->next=head; + head=n; +} +void add_tail(VSDnode* &head,int val){ + + VSDnode* n = new VSDnode(val); + if(head==NULL)/* If the head of the linked list is None, it sets the head to the newly created node */ + { + head=n; + return; + } + VSDnode* temp = head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=n; +} + +void display(VSDnode* head){ + + VSDnode* temp=head; + while(temp!=NULL){ + cout<data<<"->"; + temp=temp->next; + } + cout<<"NULL"<next; + curr->next=prev; + prev=curr; + curr=next; + } + return prev; +} +/* it compares the values of the linked list with the values popped from the stack */ +void is_palindrome(VSDnode* head){ + VSDnode* new_head=reverse(head); + VSDnode* temp1=head; + VSDnode* temp2=new_head; + while(temp1){ + if(temp1->data != temp2->data){ + cout<<"is not a palindrome"<next; + temp2=temp2->next; + } + cout<<"is a palindrome"<