左神基础班-单链表存储的数,判断回文

先上时间复杂度和空间复杂度 都是 O(n)的解法

 

里面的几个函数功能分别为:1.由数组生成链表、2.得到反转后的链表、3.顺序打印链表,4.比较两个链表,若有一处不同,则不是回文。

#include
#include
using namespace std;
struct LinkNode{
	int value;
	LinkNode* next;
};
LinkNode* getLink(int arr[], int length);
LinkNode* getLink_reverse(LinkNode* head1, int length);
void show(LinkNode* head);
bool compare(LinkNode* head1, LinkNode* head2);

int main(){
	int arr1[] = {1,2,3,2,1};
	LinkNode* head1 = getLink(arr1,5);
	LinkNode* head2 = getLink_reverse(head1, 5);
	// show(head1);
	// show(head2);
	cout << "is huiwen: "<< compare(head1, head2);
	return 0;
}

LinkNode* getLink(int arr[], int length){
	LinkNode* head = new LinkNode();
	LinkNode* go = head;
	int i =0;
	while(ivalue = arr[i];
		node->next = nullptr;
		go->next = node;
		go = go->next;
		i++;
	}
	return head;	
}
LinkNode* getLink_reverse(LinkNode* head1 , int length){
	stack s;
	LinkNode* go = head1->next;
	while(go!=nullptr){
		s.push(go->value);
		go=go->next;
	}
	LinkNode* head2 = new LinkNode();
	LinkNode* go2 = head2;
	while(!s.empty()){
		LinkNode* node = new LinkNode();
		node->value = s.top();
		s.pop();
		node->next = nullptr;
		go2->next = node;
		go2 = go2->next;
	}
	return head2;
}
void show(LinkNode* head){
	LinkNode* go = head->next;
	while(go!=nullptr){
		cout << go->value << endl;
		go = go->next;
	}
}
bool compare(LinkNode* head1, LinkNode* head2){
	LinkNode* go1 = head1->next;
	LinkNode* go2 = head2->next;
	while((go1!= nullptr && go2 != nullptr) &&
			 go1->value == go2->value){
		go1 = go1->next;
		go2 = go2->next;
	}
	return go1==nullptr? true : false;
}

+————————————————————————————————————————————————————————+

 

下面贴时间复杂度位O(n),空间复杂度为O(1)的代码

 

#include
#include
using namespace std;
struct LinkNode{
	int value;
	LinkNode* next;
};
LinkNode* getLink(int arr[], int length);
void show(LinkNode* head);
bool judge(LinkNode* head);
int main(){
	int arr1[] = {1,2,3,3,2,1};
	LinkNode* head1 = getLink(arr1,6);
	// show(head1);
	cout << "is huiwen: " << judge(head1)<value = arr[i];
		node->next = nullptr;
		go->next = node;
		go = go->next;
		i++;
	}
	return head;	
}
void show(LinkNode* head){
	LinkNode* go = head->next;
	while(go!=nullptr){
		cout << go->value << endl;
		go = go->next;
	}
}

bool judge(LinkNode* head){
	LinkNode* slow;
	LinkNode* fast;
	slow = fast = head->next;
	while(fast->next != nullptr && fast->next->next != nullptr){
		slow = slow->next;
		fast = fast->next->next;
	}
	//此时,slow指向中间的节点
	fast = slow->next;
	slow->next = nullptr;
	LinkNode* help = fast->next;
	while(help != nullptr){
		fast->next = slow;
		slow = fast;
		fast = help;
		help = help->next;	
	}
	fast->next = slow;
	//反转指针完毕,判断回文情况
	LinkNode* front = head->next;
	LinkNode* tail = fast;
	while(front !=nullptr ){
		if(front->value != tail->value){
			return false;
		}
		// cout << "now ,front is:" << front->value <<" tail is: "<< tail->value << endl;
		front = front->next;
		tail = tail->next;
	}
	//把指针调整回去
	slow = fast;
	fast = fast->next;
	slow->next = nullptr;
	help = fast->next;
	while(help != nullptr){
		fast->next = slow;
		slow = fast;
		fast = help;
		help = help->next;
	}
	fast->next = slow;
	return true;

}

 

来个图片帮助理解。

 

 

其中,要设立一个help指针。其指向正在被改变指针节点 的那个节点的下一个节点。

这样,当一个节点其后指针改变时。还能找到其原先的后一个节点,方能继续修改。

 

记得如果是回文数的话要把 链表中的指针修正回来哦?+~

你可能感兴趣的:(左神基础班代码,C++)