Header Ads Widget

Responsive Advertisement

Custom Palindrome of a link list


 A custom palindrome refers to a sequence, structure, or algorithm crafted to determine whether a string, word, or data sequence reads the same backward as forward, often tailored to specific applications or requirements.

Historical Context of Palindromes

Ø  Palindromes have been a part of literary, mathematical, and computer science traditions for centuries.

Ø  The word itself is derived from the Greek "palindromos," meaning "running back again."

Ø  Early examples in literature include phrases, poems, and religious texts. In mathematics, palindromic numbers and sequences are well-studied.

Custom Palindrome in Computer Science

  1. Algorithms:

Ø  Typically involve comparing the sequence from the start and end toward the middle.

Ø  Custom implementations might account for:

ü  Case insensitivity.

ü  Ignoring non-alphanumeric characters.

ü  Handling nested structures or linked lists (e.g., palindrome-linked lists).

  1. Applications:

Ø  Validating data symmetry in programming.

Ø  Testing systems' functionality in bioinformatics for DNA sequences.

Ø  Enhancing user interaction in games or puzzles.

package com.kartik.linklist;

public class Palindrome<T> {

 public static class Node<T> {

    // This signifies the reference to the next node in the series, or null if there is no additional node.

    Node<T> next;

    // The data associated with this node may be of any type you require.

    T data;

    // Constructor for the Node class

    public Node(T dataValue) {

     next = null;

     data = dataValue;

    }

    // Alternative constructor allowing specification of the next node.

    public Node(T dataValue, Node<T> nextValue) {

     next = nextValue;

     data = dataValue;

    }

    // Method to retrieve the data from the node

    public T getData() {

     return data;

    }

    public void setData(T dataValue) {

     data = dataValue;

    }

    public Node<T> getNext() {

     return next;

    }

    public void setNext(Node<T> nextValue) {

     next = nextValue;

    }

   }

   private Node<T> head;

   private Node<T> tail;

   /**

    * Default constructor for the Palindrome class.

    */

   public Palindrome() {

   }

  /**

* Constructor that initializes the list with a specified element.

*

* @param data The initial data for the list.

*/

   Palindrome(T data) {

    Node<T> n1 = new Node<T>(data);

    this.head = n1;

    this.tail = n1;

   }


   /**

* Adds the specified element to the end of the list.

*

* @param data The data to be added.

*/


   public void add(T data) {

    // If the list is empty, create a new list and set its head and tail.

    if (this.head == null) {

     Palindrome<T> linkList = (new Palindrome<T>(data));

     this.head = linkList.head;

     this.tail = linkList.tail;

     return;

    }

    // Create a new node to be added at the end of the list.

    Node<T> n = new Node<T>(data);

    Node<T> current = this.head, prev = null;

    while (current != null) {

     prev = current;

     current = current.next;

    }

    prev.next = n;

    this.tail = n; // Update the tail information as well.

   }

   public String toString() {

    String output = "";

    if (head != null) {

     Node<T> n = head.getNext();

     output += "[" + head.getData().toString() + "]-->";

     while (n != null) {

      output += "[" + n.getData().toString() + "]-->";

      n = n.getNext();

     }

    }

    return output;

   }

/**

* <ul>

* <li>The Tortoise (slow pointer) and the Hare (fast pointer) begin at the

* start of the linked list.</li>

* <li>During each iteration, the slow pointer advances by one step while the fast pointer advances by two steps.</li>

* <li>If a loop exists, the fast pointer will circle around the loop and eventually meet the slow pointer.</li>

* <li>If no loop is present, the fast pointer will reach the end of the list without encountering the slow pointer.</li>

* </ul>

*

* @purpose This function identifies the middle element in a linked list.

* */

   public Node<T> findMiddleNode(Node<T> head) {

    Node<T> slowPointer, fastPointer;

    slowPointer = fastPointer = head;

    while (fastPointer != null) {

     fastPointer = fastPointer.next;

     if (fastPointer != null && fastPointer.next != null) {

      slowPointer = slowPointer.next;

      fastPointer = fastPointer.next;

     }

    }

    return slowPointer;

   }


/**

* <ul>

* <li>Initially, determine the middle node of the linked list.</li>

* <li>Next, reverse the second half of the linked list from the middle to the end.</li>

* <li>Finally, compare the first node with the last node of the reversed section.</li>

* </ul>

*

* @purpose Function to verify whether the linked list is a palindrome.

* @return

*/

   public boolean checkPalindrome() {

    Node<T> fastPtr = head;

    //Identify the middle node by employing the slow and fast pointer technique. 

    Node<T> middleNode = findMiddleNode(fastPtr);

    // This will lead us to the head of the second segment,

    Node<T> secondHead = middleNode.next;

    // marking the conclusion of the first segment of the linked list.

    middleNode.next = null;

    // Subsequently, obtain the reversed linked list for the second segment.

    Node<T> reverseSecondHead = reverseLinkedList(secondHead);

    while (fastPtr != null && reverseSecondHead != null) {

     if (fastPtr.data == reverseSecondHead.data) {

      fastPtr = fastPtr.next;

      reverseSecondHead = reverseSecondHead.next;

      continue;

     } else {

      return false;

     }

    }

    return true;

   }

/**

* @purpose This function is designed to reverse a linked list.

* @param currentNode

* The current node being processed.

* @return The previousNode of the Node<T> object.

*/

   public Node<T> reverseLinkedList(Node<T> currentNode) {

   // For the initial node, previousNode will be set to null.

    Node<T> previousNode = null;

    Node<T> nextNode;

    while (currentNode != null) {

     nextNode = currentNode.next;

     // Reversing the link direction.

     currentNode.next = previousNode;

     // Advancing both currentNode and previousNode by one node.

     previousNode = currentNode;

     currentNode = nextNode;

    }

    return previousNode;

   }


 public static void main(String[] args) {

   System.out.println("------------------Integer-------------------");

      Palindrome<Integer> list = new Palindrome<Integer>();

      list.add(5);

      list.add(6);

      list.add(3);

      list.add(3);

      list.add(6);

      list.add(5);

      System.out.println(list.toString());

      System.out.println("Linked list palidrome: "+list.checkPalindrome());

      System.out.println();

      System.out.println("------------------String-------------------");

      Palindrome<String> list2 = new Palindrome<String>();

      list2.add("Kartik");

      list2.add("Mandal");

      list2.add("Saharda");

      list2.add("kharagpur");

      list2.add("Westbengal");

      list2.add("India");

      list2.add("India");

      list2.add("Westbengal");

      list2.add("kharagpur");

      list2.add("Saharda");

      list2.add("Mandal");

      list2.add("Kartik");

      System.out.println(list2.toString());

      System.out.println("Linked list palidrome: "+list2.checkPalindrome());

   }

 }


 

 



The Java code I've provided is an implementation of a generic Palindrome class for a singly linked list. This implementation allows checking whether the linked list is a palindrome. A palindrome is a sequence that reads the same forward and backward.

Key Components and Explanation:

  1. Node Class:
    1. Represents an individual node in the linked list.
    2. Contains a reference (next) to the next node and a data field (data) to hold the value of the node.
    3. Two constructors are provided to initialize nodes with or without specifying the next node.
  2. Palindrome Class:
    1. The Palindrome class is a generic class that can work with any type (T) of data.
    2. The class maintains references to the head and tail of the linked list.
  3. add Method:
    1. This method adds a new node to the end of the linked list. If the list is empty, it initializes the list with the new node as both head and tail.
  4. toString Method:
    1. This method generates a string representation of the linked list, showing the sequence of nodes.
  5. findMiddleNode Method:
    1. This method finds the middle node of the linked list using the slow and fast pointer technique. The slow pointer moves one step at a time, while the fast pointer moves two steps. When the fast pointer reaches the end, the slow pointer will be at the middle node.
  6. reverseLinkedList Method:
    1. This method reverses the linked list starting from the given node. It iterates through the list, reversing the next pointers to point to the previous node.
  7. checkPalindrome Method:
    1. This method checks if the linked list is a palindrome. It works by:
      1. Finding the middle node of the list.
      2. Reversing the second half of the list.
      3. Comparing nodes from the start and the reversed second half.
    2. If all corresponding nodes are equal, the list is a palindrome; otherwise, it is not.
  8. main Method:
    1. The main method tests the palindrome functionality with both Integer and String linked lists.
    2. It adds elements to the list, prints the list, and checks whether the list is a palindrome.

Example Output:

If you run this program, the output might look like this:

plaintext

------------------Integer-------------------

[5]-->[6]-->[3]-->[3]-->[6]-->[5]-->

Linked list palindrome: true

 

------------------String-------------------

[Kartik]-->[Mandal]-->[Saharda]-->[kharagpur]-->[Westbengal]-->[India]-->[India]-->[Westbengal]-->[kharagpur]-->[Saharda]-->[Mandal]-->[Kartik]-->

Linked list palindrome: true

 

How It Works:

For the integer list [5, 6, 3, 3, 6, 5], the list is a palindrome, so the output is true.
For the string list ["Kartik", "Mandal", "Saharda", "kharagpur", "Westbengal", "India", "India", "Westbengal", "kharagpur", "Saharda", "Mandal", "Kartik"], the list is also a palindrome, so the output is true.

This implementation provides a clear and efficient way to check if a linked list is a palindrome, using common linked list operations like finding the middle node, reversing a list, and comparing node values.

 

For More Spring Related information, visit

Ø  Mastering Debounce, Throttle, Rate Limit & Backoff in Java

Ø  Deep understand of ThrottlingFilter with RewritePath filter in cloud gateway

Ø  Setting up Custom Filters using Debouncing, Throttling, Rate Limiting, and Exponential Backoff

Ø  Custom gateway filters in Spring Cloud Gateway

Ø  Custom Filters in Microservices

Ø  Mastering Debounce, Throttle, Rate Limit & Backoff in Java

Ø  Microservices: Custom Filters for Debouncing, Throttling, Rate Limits & Backoff

Ø  Spring Cloud Gateway uses a RewritePath filter

Ø  How to call rest api from java code

Ø  How to use Http Client

Ø  Key Components of Apache Kafka for Scalable Messaging

Ø  Build a Video Stream Microservice with Kafka & REST API in Java

Ø  Kafka general questions and answers

 

For More DSA Related information, visit

Ø  Bench mark of compiler using Ackerman function

Ø  Find the Missing Number

Ø  To check if the rows of a matrix are circularly identical in Java

Ø  how to check loop in array

Ø  100 door puzzle programs

Ø  Frequency Weaving Logic & Spiral Printing of a Rectangle

Ø  Zig Zag Matrix print multiple way

Ø  Greedy Algorithm’s or knapsack algorithms

Ø  understanding recursive method for binary tree

Ø  Dynamic Programming: Max Square Sub-matrix of 1s in a Matrix

Ø  Previous and Next Date Palindrome

Ø  Karatsuba's Algorithm for multiplying two large numbers

Ø  Multiplication In different Way

Ø  Division by Different way

Ø  How to draw a Tree from array of integer

Ø  Position of robot after given movements

Ø  Alphanumeric Random Number generator

Ø  Solving Tiger-Goat-Boatman Puzzle: BFS & DFS River Crossing

Ø  Dijsktra Shortest Path for directed an undirected graph

 

For More Java Related information, visit

Ø  Streams Lambdas Collectors and Functional Interfaces in Java 8

Ø  Java 8 support static method or default method or both in the interface

Ø  Inheritance Understand

Ø  Serialization understanding

Ø  Clone Under standing

Ø  Exception understanding

Ø  Garbage Collection Under Standing

Ø  How work Garbage Collection in Java

Ø  Under Standing Of Mutable and Immutable Class

Ø  enum understand

 

For More sort information, visit:

Ø  Selection Sort with iteration wise per step

Ø  Insertion Sort

Ø  Bubble Sort with each iteration how it is work

Ø  Merge sort of Each step how it is working

Ø  Quick Sort per iteration what happen

Ø  Sorting of country

Ø  Sorting Of a list multiple attribute wise two technique

Ø  Seat Arrangement in Sorting Order Like 1A-1E, 3C-3G etc

Ø  How to sort 10 billion numbers

Ø  Merge Sort simple under standing

 

For Math information, visit:

Ø  Calculating the area of a triangle

For Design information, visit:

Ø  Design pattern

Ø  Mastering Design Patterns Practical Implementation Tips

Ø  How to draw sequence diagram and other diagrams using plantuml

Ø  Time Analysis for Congested Routes Using Shortest Path Algorithms

Ø  Java Design Pattern


For Custom information, visit:

Ø  Custom ArrayList By Java Source code

Ø  Custom SinglyLinkList By java code

Ø  Custom Doubly LinkList By java

Ø  Custom Stack using an Array in Java code

Ø  HTTPS and HTTP information

Ø  Custom Reverse Linked List

Ø  Custom Combination and permutation program

Ø  Custom middle Element of array

Ø  Find Middle & Reverse a Custom Linked List Using Iteration

Ø  Custom binary tree Printer

Ø  Custom Binary Search

Ø  Detect & Handle Infinite Loops and Cycles in Linked Lists

Ø  Custom Palindrome of a link list

Ø  Creating a custom HashMap in Java

Ø  Custom Combination and permutation program

 

For Security information, visit:

Ø  Algorithm for HashMac

Ø  Asymmetric Encryption: Public Key for Encryption, Private for Decryption

Ø  Symmetric: Encryption and decryption by same key

Ø  Generating keystore files

Ø  Asynchronous Encryption and decryption without file only key pass

Ø  public key encryption and private key decryption with keystore file

Ø  OWASP (Open Web Application Security Project)

Ø  To securely obtain employee information utilizing TLS 1.3 or TLS 1.2

Ø  TLS 1.3 Configuration

For Tools information, visit:

Ø  Auto-Update Batch File with Latest JAR & Start App Automatically

Ø  Connectingto IBM WebSphere MQ in Java

Ø  How to create maven project

Ø  VisualVM monitoring like jconsole

Ø  Stylus studio convert edifact message

Ø  JConsole Monitoring for Java Standalone or Web application project

Ø  Apache Cluster router load blancer

 

For Cloud information, visit:

Ø  creating a hierarchical diagram for cloud logging

Ø  A hierarchical structure that includes a broader range of google cloud services


For Chemistry information, visit:

Ø  Molecular weight of chemistry in Java code

Ø  To generate a chemical formula look using HTML

Ø  Orbitals and Electron Configuration Hunds Rule

For Other information, visit

Ø  String to xml or html Beautifier

Ø  How to convert XML to Object and Object to XML

Ø  Convert Floating-Point Values from SQL Server to Oracle in Java




Custom Palindrome of a link list
Custom Palindrome of a link list








Post a Comment

0 Comments