Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using a Linked List structure, determine whether a linked list contains a Palind

ID: 3814255 • Letter: U

Question

Using a Linked List structure, determine whether a linked list contains a Palindrome in integers. For example, the following is a Palindrome:

0-> 1 -> 2 -> 1 -> 0

To solve this problem, following these steps:

1.) reverse the linked list

2.) compare the reversed list to the original list

3.) if they're the same, you have a Palindrome.


Create the following methods in a driver class, and call them from main:

boolean isPalindrome(LinkedList aLL) ~ given a linked list, will return whether the linked list is a Palindrome.

LinkedList reverseAndClone(LinkedList aLL) ~ given a linked list, will return the reversed linked list.

boolean isEqual(LinkedList one, LinkedList two) ~ given the 2 linked lists, will return whether they are both the same.

Define 2 global linkedList variables that can be accessed from anywhere, one for origLinkedList and the other for reversedLinkedList.

To test the program, in main method:

1. create a linked list that is a Palindrome, and call the isPalindrome method. Test to ensure it returns True.
2. create a linked list that is NOT a Palindrome, and ensure the call to isPalindrome returns a False.

Explanation / Answer

HI, Please find my implementation.

Please let me know in case of any issue.

import java.util.LinkedList;

import java.util.Scanner;

public class LinkedListPalindrome {

  

   public static boolean isPalindrome(LinkedList<Integer> aLL){

      

       LinkedList<Integer> reverse = reverseAndClone(aLL);

      

       if(isEqual(aLL, reverse))

           return true;

      

       return false;

      

   }

  

   public static LinkedList<Integer> reverseAndClone(LinkedList<Integer> aLL){

      

       LinkedList<Integer> reverse = new LinkedList<>();

      

       for(int i=aLL.size()-1; i>=0; i--){

           reverse.add(aLL.get(i));

       }

      

       return reverse;

   }

  

  

   public static boolean isEqual(LinkedList<Integer> one, LinkedList<Integer> two){

      

       for(int i=0; i<one.size(); i++)

           if(one.get(i) != two.get(i))

               return false;

      

       return true;

   }

  

   public static void main(String[] args) {

      

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

      

       Scanner sc = new Scanner(System.in);

      

       System.out.println("Enter numbers(-1 to stop): ");

       while(true){

          

           int num = sc.nextInt();

          

           if(num == -1)

               break;

          

           list.add(num);

       }

      

       System.out.println("isPallindrome ? "+isPalindrome(list));

   }

}

/*

Sample run:

Enter numbers(-1 to stop):

1 2 3 3 2 1 -1

isPallindrome ? true

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote