Rotated Palindrome Linked List 2025 (Required)
Time limit: 5000ms
Memory limit: 256mb
Description:
In this exercise, we practise rotation on a singly linked list. Rotating a singly linked list by 1 position means that the first node becomes the last node and all other nodes move one step forward. For example: rotating a linked list 1 -> 2 -> 3 -> 4 by one time turns it to 2 -> 3 -> 4 -> 1. You also need to know the concept of palindrome: A palindrome is a sequence that reads the same forward and backward.
The user will provide m positive integers, each to be prepended to the linked list sequentially. Your task is find out the value n >= 0, the minimum number of rotations required so that a linked list can be made to become palindrome. If there does not exist such an n, output -1.
You will complete the four functions below.
void prepend(List head, int value);
- This function prepends value to the list pointed by head.
void print(List head);
- Print all integers of linked list p from head to tail in one line.
- Every two integers are separated by " -> ". Print a '\n' in the end.
- If the list contains no integers, output a blank line.
int isPalindrome(List* l);
- Determine whether a given linked list is palindrome or not
void rotateNTimes(List head, int n);
- This function rotates the list pointed by head for n times.
int getMinRotations(List* l)
- This function calculates the minimum rotation times so that a linked list can be made to become a palindrome. If impossible, return -1
Sample Input 1:
4
3 9 10 4
Sample Output 1:
Input m:
Input the numbers to be prepended sequentially:
Original linked list:
3 -> 9 -> 10 -> 4
The linked list is not a palindrome linked list.
The linked list can't be made to become a palindrome linked list through rotations.
Sample Input 2:
3
1 2 1
Sample Output 2:
Input m:
Input the numbers to be prepended sequentially:
Original linked list:
1 -> 2 -> 1
The linked list itself is a palindrome linked list.
Sample Input 3:
5
1 1 2 3 2
Sample Output 3:
Input m:
Input the numbers to be prepended sequentially:
Original linked list:
1 -> 1-> 2 -> 3 -> 2
The linked list is not a palindrome linked list.
The linked list can be made to become a palindrome linked list through 1 rotation, which is:
1 -> 2 -> 3 -> 2 -> 1
Sample Input 4:
7
8 4 1 1 4 8 2
Sample Output 3:
Input m:
Input the numbers to be prepended sequentially:
Original linked list:
8 -> 4-> 1 -> 1 -> 4 -> 8 -> 2
The linked list is not a palindrome linked list.
The linked list can be made to become a palindrome linked list through 3 rotations, which is:
1 -> 4 -> 8 -> 2 -> 8 -> 4 -> 1
-------------------------Copy the following code, complete it and submit-------------------------
/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct linked_list_node{
int value;
struct linked_list_node* next;
} Node;
typedef struct linked_list{
Node* head;
Node* tail;
} List;
void initList(List* l){
l->head = NULL;
l->tail = NULL;
}
void prepend(List* l, int value){
// code here
}
void print(List* l){
// code here
}
int isPalindrome(List* l) {
// code here
}
void rotateNTimes(List* l, int n){
// code here
}
int getMinRotations(List* l) {
// code here
}
int main() {
List* l = (List*) malloc(sizeof(List));
initList(l);
int i, m;
int value;
printf("Input m:\n");
scanf("%d", &m);
printf("Input the numbers to be prepended sequentially:\n");
for (i = 0; i < m; i++) {
scanf("%d", &value);
prepend(l, value);
}
printf("Original linked list:\n");
print(l);
if (! isPalindrome(l)){
printf("The linked list is not a palindrome linked list.\n");
}
int n = getMinRotations(l);
if (n > 0) {
printf("The linked list can be made to become a palindrome linked list through %d rotations, which is:\n", n);
print(l);
} else if (n == 0) {
printf("The linked list itself is a palindrome linked list.\n");
} else {
printf("The linked list can't be made to become a palindrome linked list through rotations.\n");
}
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Submit