C/C++之判断链表是否是回文链表

判断链表是否是回文链表主要通过判断对应位置的元素值是否是相等的

第一种解法:将原有链表反转获取新的链表,与原有的链表比较,判断对应元素的值是否相等

#include <iostream>
using namespace std ;
struct node
{
	int data ;
	struct node *next ;
};
typedef struct node *PLIST;
typedef struct node NODE;
PLIST createList_link()
{
	PLIST head=new struct node;
	head->next=NULL;
	return head ;
}
void insertTail(PLIST llist,int x)
{
	PLIST tail=llist->next;
	PLIST temp=new struct node;
	temp->data=x;
	temp->next=NULL;
	if(tail==NULL)
	{
		llist->next=temp;
		return;
	}
	else
	{
		while(tail->next!=NULL)
		{
			tail=tail->next;
		}
	}
	tail->next=temp;
}
void insertHead(PLIST llist,int x)
{
	PLIST tail=llist->next;
	PLIST temp=new struct node;
	temp->data=x;
	temp->next=tail;
	llist->next=temp;
}
void printList_link(PLIST head)
{
    for(PLIST p=head->next;p!=NULL;p=p->next){
        cout<<p->data<<" ";
    }
    cout<<endl;
}
PLIST reverseList_link(PLIST head)
{
	PLIST p=head->next;
	PLIST copy=createList_link();
	while(p!=NULL)
	{
		insertHead(copy,p->data);
		p=p->next;
	}
	return copy;
}
int main()
{
	//begin 生成链表 
	int flag=1,arr[]={3,324,34,34,324,3};
	PLIST head=createList_link();
	for(int i=0;i<6;i++)
	{
		insertHead(head,arr[i]);
	}
	PLIST copy=reverseList_link(head);
	for(PLIST p=head->next,q=copy->next;p!=NULL;p=p->next,q=q->next)
	{
		if(p->data!=q->data)
		{
			flag=0;
			break;
		}
	}
	if(flag)
		cout<<"Yes";
	else
		cout<<"No";
	//end 
	
	return 0;
} 

第二种解法:借助栈结构和双指针,一个指针的位移为1并将此时访问的元素放到栈中,另一个指针的位移为2,当位移为2的指针到达末尾时,位移为1的指针到达链表中间位置。此后将栈中元素与链表中元素值比对

#include <iostream>
#include<stack>
using namespace std ;
struct node
{
	int data ;
	struct node *next ;
};
typedef struct node *PLIST;
typedef struct node NODE;
PLIST createList_link()
{
	PLIST head=new struct node;
	head->next=NULL;
	return head ;
}
void insertTail(PLIST llist,int x)
{
	PLIST tail=llist->next;
	PLIST temp=new struct node;
	temp->data=x;
	temp->next=NULL;
	if(tail==NULL)
	{
		llist->next=temp;
		return;
	}
	else
	{
		while(tail->next!=NULL)
		{
			tail=tail->next;
		}
	}
	tail->next=temp;
}
void printList_link(PLIST head)
{
    for(PLIST p=head->next;p!=NULL;p=p->next){
        cout<<p->data<<" ";
    }
    cout<<endl;
}
int main()
{
	//begin 生成链表 
	int flag=1,arr[]={3,324,34,5,34,324,3};
	PLIST head=createList_link();
	for(int i=0;i<7;i++)
	{
		insertTail(head,arr[i]);
	}
	//end
	stack<int> s; 
	PLIST n1=head->next,n2=head->next->next;
	while(n2!=NULL&&n2->next!=NULL)
	{
		s.push(n1->data);
		n1=n1->next;
		n2=n2->next->next;
	}
	if(n2!=NULL&&n2->next==NULL)
	{
		s.push(n1->data);
		n1=n1->next;
	 } 
	 else if(n2==NULL)
	{
		n1=n1->next;
	}
	 	
	while(n1!=NULL)
	{
		if(n1->data!=s.top())
		{
			flag=0;
			break;
		}
		s.pop();
		n1=n1->next;
	}
	if(flag)
		cout<<"Yes";
	else
		cout<<"No";
	return 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、付费专栏及课程。

余额充值