比较2个链表的数据! (JAVA)

时间:2022-04-25 22:51:28

My project is to see if a user inputted string is a palindrome or not. Firstly, the string is to be put into a Linked list and then copied into a second linked list where I reverse the list completely in order to compare them.

我的项目是看用户输入的字符串是否是回文。首先,将字符串放入链接列表,然后复制到第二个链接列表中,我完全反转列表以进行比较。

the linked list is my own class as well all the methods within it. I have confirmed that the linked list works as does the reverse method.

链表是我自己的类以及其中的所有方法。我已经确认链表和反向方法一样工作。

Where i run into problems is when i try and compare the 2 from the beginning of the lists. This is what i've tried:

我遇到问题的地方是当我尝试比较列表开头的2时。这就是我尝试过的:

package palindromes;
import data_structures.*;
import java.util.Iterator;

public class Palindrome
{
    public boolean isPalindrome(String s)
    {
        if (s.length() <= 1) // base case
            return true;
        String originalList = s.replaceAll("[\\W]", "").toLowerCase();

        LinkedList<Character> list1 = new LinkedList<Character>();
        LinkedList<Character> rlist = new LinkedList<Character>();
        for (int i = 0; i < originalList.length(); i++)
        {
            list1.addLast(originalList.charAt(i));
        }
        rlist = list1;
        rlist.reverse();
        Iterator<Character> l1 = list1.iterator();
        Iterator<Character> l2 = rlist.iterator();
        while (l1.hasNext() && l2.hasNext())
        {
            if (l1.next() != l1.next())
                return false;
        }
        return true;
    }
}

I try to iterate through both lists in the while loop but when i go to test the data of the nodes it is incorrect.

我尝试迭代while循环中的两个列表但是当我去测试节点的数据时它是不正确的。

Please let me know if you need added information or clarification.

如果您需要补充信息或说明,请与我们联系。

My question is how do i fix my code so that i am able to compare the lists sequentially to see if the inputted string is a palindrome or not?

我的问题是我如何修复我的代码,以便我能够顺序比较列表,看看输入的字符串是否是回文?

2 个解决方案

#1


2  

This can be achieved in following way:

这可以通过以下方式实现:

     List<String> list = new ArrayList<String>();

     list.add("a");
     list.add("b");
     list.add("c");
     list.add("c");
     list.add("b");
     list.add("a");
     int size = list.size();
     for(int i = 0; i<size/2; i++)
     {
         if(list.get(i) == list.get(size - 1))
            System.out.println("list is palindrome");

     }

#2


0  

rlist = list1;

is the problem. Both rlist and list1 are just two references to the same List.

是问题。 rlist和list1都只是对同一个List的两个引用。

replace it with

用它替换它

rlist = new LinkedList<Character>(list1);

this will create new List and copy all the elements in the list1 to that new list. rlist is referring to the new list while list1 refers to the old list.

这将创建新的List并将list1中的所有元素复制到该新列表中。 rlist指的是新列表,而list1指的是旧列表。

#1


2  

This can be achieved in following way:

这可以通过以下方式实现:

     List<String> list = new ArrayList<String>();

     list.add("a");
     list.add("b");
     list.add("c");
     list.add("c");
     list.add("b");
     list.add("a");
     int size = list.size();
     for(int i = 0; i<size/2; i++)
     {
         if(list.get(i) == list.get(size - 1))
            System.out.println("list is palindrome");

     }

#2


0  

rlist = list1;

is the problem. Both rlist and list1 are just two references to the same List.

是问题。 rlist和list1都只是对同一个List的两个引用。

replace it with

用它替换它

rlist = new LinkedList<Character>(list1);

this will create new List and copy all the elements in the list1 to that new list. rlist is referring to the new list while list1 refers to the old list.

这将创建新的List并将list1中的所有元素复制到该新列表中。 rlist指的是新列表,而list1指的是旧列表。