Exemplo n.º 1
0
SinglyLinkedList SinglyLinkedList::mergeTwoSortedLinkedList(SinglyLinkedList a,SinglyLinkedList b){
	SinglyLinkedList c;
	SingleNode *head1=a.head;
	SingleNode *head2=b.head;
	SingleNode *head3=c.head;
	while(head1 && head2){
		if(head1->data <= head2->data){
			c.insertAtEnd(head1->data);
			head1=head1->next;
		}
		else{
			c.insertAtEnd(head2->data);
			head2=head2->next;
		}
	}
	if(head1){
		while(head1){
			c.insertAtEnd(head1->data);
			head1=head1->next;
		}
	}

	if(head2){
			while(head2){
				c.insertAtEnd(head2->data);
				head2=head2->next;
			}
		}
	return c;
}
Exemplo n.º 2
0
bool SinglyLinkedList::checkIfListPalindrome(){
	SinglyLinkedList reversed;
	SingleNode *tmp = head;
	while(tmp != NULL){
		reversed.insertAtEnd(tmp -> data);
		tmp = tmp -> next;
	}
	reversed.printSinglyLinkedList();
	reversed.reverseSinglyLinkedList();
	reversed.printSinglyLinkedList();
	SingleNode *tmp1 = head;
	SingleNode *tmp2 = reversed.head;
	while(tmp1!=NULL){
		if(tmp1->data != tmp2->data)
			return false;
		else{
			tmp1 = tmp1 -> next;
			tmp2 = tmp2 ->next;
		}
	}
	return true;
}