判断链表是否是回文数

public class IsPanlindromeList {
public static class Node{
Node next ;
int value;
public Node(int value){
this.value = value;
}
}
//第一种方法,用了空间复杂度为n,将链表压入栈中,之后将链表和栈中的元素逐个比对。
public static boolean isPanlindromeList1(Node head){
if(head==null&&head.next==null){
return true;
}
Stack<Node> stack = new Stack<>();
Node cur = head;
while (cur!=null){
stack.push(cur);
cur = cur.next;
}
while (head!=null){
if(head.value!=stack.pop().value){
return false;
}
head = head.next;
}
return true;
}
public static void printLinkedList(Node node) {
System.out.print("Linked List: ");
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
}
//第二种方法。先求出链表的中间值,之后把后面的数据压入栈中,接下来,进行弹出比对。
public static boolean isPanlindromeList2(Node head){
if(head==null||head.next==null){
return true;
}
Node pre = head;
Node cur = head;
while (cur.next!=null&&cur.next.next!=null){
pre = pre.next;
cur = cur.next.next;
}
Stack<Node> stack = new Stack<>();
while (pre.next!=null){
stack.push(pre.next);
pre.next =pre.next.next;
}
while (!stack.isEmpty()){
if (stack.pop().value!=head.value){
return false;
}
}
return true;
}

//第三种方法,不借助辅助空间判断是不是回文数。
//先找到中间的那个,让他的指针指向null。之后把后面的链表部分反转。接下来在从两端开始进行比较
//记得把两端的数据保存起来。之后复原链表。
public static boolean isPanlindromeList3(Node head){
if(head==null||head.next==null){
return true;
}
Node cur1 = head;
Node cur2 = head;
while (cur2.next!=null&&cur2.next.next!=null){
//这样既满足了链表为单数的情况。又满足了链表为双数的情况。
//当链表为双数时,链表的中心默认为中间两个数中偏左边的那个数。
cur1 = cur1.next;
cur2 = cur2.next.next;
}
Node cur3 = cur1.next;
cur1.next = null;
while (cur3!=null){
cur2 = cur3.next;
cur3.next = cur1;
cur1 = cur3;
cur3 = cur2;
}
cur2 = head;
cur3 = cur1;
boolean flag = true;
while (cur1!=null&&cur2!=null){
if(cur1.value!=cur2.value){
flag = false;
}
cur1 = cur1.next;
cur2 = cur2.next;
}

cur2 = cur3.next;
cur3.next = null;

while (cur1!=null){
cur1 = cur2.next;
cur2.next = cur3;
cur3 = cur2;
cur2 = cur1;
}
return flag;
}
public static void main(String[] args) {
Node node1 = new Node(2);
node1.next = new Node(3);
node1.next.next = new Node(3);
node1.next.next.next = new Node(2);
// System.out.print(isPanlindromeList1(node1));
// System.out.print(isPanlindromeList2(node1));
System.out.print(isPanlindromeList3(node1));

}

}
总结;三种方法。最后一种方法最好。

转载于:https://www.cnblogs.com/liuwentao/p/9398212.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用栈来判断链表是否为回文串是常见的算法问题,通常会先将链表转换成数组或者双向链表,然后利用栈的特性来实现。以下是使用栈的C++方法: 1. 遍历链表:首先,我们需要创建两个指针,一个正向遍历链表(`curr`),另一个反向遍历链表(`reverseCurr`)。初始时,反向指针设为链表的尾部。 2. 双向插入栈:当正向指针不为空时,将正向指针的节点值压入栈,同时移动正向和反向指针。这样可以保证栈顶元素始终包含链表中对应位置的节点。 3. 对比栈顶:当正向指针和反向指针相遇(或其中之一到达链表头)时,如果栈顶元素(正向节点)等于当前反向节点,继续比较下一个节点。如果所有节点都满足这个条件,链表就是回文的;否则不是。 4. 使用栈:C++中,可以使用`std::stack<int>`数据结构来保存节点值,`push()`用于入栈,`top()`获取栈顶元素,`pop()`移除栈顶元素。 ```cpp #include <iostream> #include <stack> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; bool isPalindrome(ListNode* head) { stack<int> s; ListNode* curr = head, *reverseCurr = new ListNode(0); reverseCurr->next = head; while (curr != nullptr && reverseCurr->next != nullptr) { s.push(curr->val); reverseCurr = reverseCurr->next; curr = curr->next; } while (reverseCurr->next != nullptr) { reverseCurr = reverseCurr->next; } while (!s.empty()) { if (s.top() != curr->val) { return false; } s.pop(); curr = curr->next; } return true; } int main() { // 测试代码省略 ListNode* list = ...; // 创建链表 if (isPalindrome(list)) { cout << "The linked list is a palindrome." << endl; } else { cout << "The linked list is not a palindrome." << endl; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值