数据结构-判断一个链表是否为回文结构-java

1.题目

给定一个链表,请判断该链表是否为回文结构。
回文是指该字符串正序逆序完全一致。

2.代码

public boolean isPail (ListNode head) {
    
    
        // write code here
        if (head!=null&&head.next==null){
    
    
            return true;
        }
        List<Integer> lists = new ArrayList<>();
        Stack<Integer> list = new Stack<>();
        while (head!=null){
    
    
            list.push(head.val);
            lists.add(head.val);
            head = head.next;
        }
        int i=0;
        while(!list.empty()){
    
    
            if (!list.pop().equals(lists.get(i))){
    
    
                return false;
            }
            i++;
        }
        return true;
  }

猜你喜欢

转载自blog.csdn.net/qq_25064691/article/details/121318285