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;
}
free(temp);
return flag;
}

Given a linked list, write a function to determine if the list is sym.pdf

  • 1.
    Given a linkedlist, 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;
  • 2.