Embed presentation
Download to read offline
![Given a linked list, write a function to determine if the list is symmetric. A list [l_1, l_2, ...., l_n]
is symmetric if l_1 = l_n, l_2 = l_n - 1, etc. Note that n must be an even number.
Solution
To do this:
struct node
{
char data;
struct node* next;
}*root, *top;
top =NULL; //head of stack
//pass root of linked list
bool ifSymmetric(node *root) {
node *tmp;
tmp = new (struct node);
temp = root; //use for first traversal
flag = 1;
//store in stack
while(temp != NULL) {
tmp->data = temp->data;
tmp->next = top;
top = tmp;
temp = temp->next;
}
//pop from stack
while(root != NULL) {
tmp = top;
if(root->data!=temp->data){
flag =0;
break;
}
top = top->next;
root = root->next;](https://image.slidesharecdn.com/givenalinkedlistwriteafunctiontodetermineifthelistissym-230702112449-793df52e/75/Given-a-linked-list-write-a-function-to-determine-if-the-list-is-sym-pdf-1-2048.jpg)

The document outlines a method to determine if a linked list is symmetric, contingent on the list having an even number of elements. It provides a function implemented in C/C++ that uses a stack to compare the first half of the list with the second half. The function returns a boolean indicating whether the list meets the symmetry condition.
![Given a linked list, write a function to determine if the list is symmetric. A list [l_1, l_2, ...., l_n]
is symmetric if l_1 = l_n, l_2 = l_n - 1, etc. Note that n must be an even number.
Solution
To do this:
struct node
{
char data;
struct node* next;
}*root, *top;
top =NULL; //head of stack
//pass root of linked list
bool ifSymmetric(node *root) {
node *tmp;
tmp = new (struct node);
temp = root; //use for first traversal
flag = 1;
//store in stack
while(temp != NULL) {
tmp->data = temp->data;
tmp->next = top;
top = tmp;
temp = temp->next;
}
//pop from stack
while(root != NULL) {
tmp = top;
if(root->data!=temp->data){
flag =0;
break;
}
top = top->next;
root = root->next;](https://image.slidesharecdn.com/givenalinkedlistwriteafunctiontodetermineifthelistissym-230702112449-793df52e/75/Given-a-linked-list-write-a-function-to-determine-if-the-list-is-sym-pdf-1-2048.jpg)
