Hello,
Give a Θ(n)-time nonrecursive procedure that reverses a singly linked list of n elements. The procedure should use no more than constant storage beyond that needed for the list itself.
The idea to reverse the linked list is the following. Keep an empty linked list as the reverse linked list. Iterate over the original list, and for each node, add it to the front of the reverse linked list. This way the order of the elements in the original linked list gets reversed, hence the linked list gets reversed.
Note that each element in the linked list is accessed once,
hence the time complexity will be
. The only
additional storage used is to iterate over the original linked
list, which will take the space of one node. Hence, it is a
constant amount.
Here is the code with additional code for testing.
# include <iostream>
class Node{
public:
int data;
Node* next;
Node() {
next = NULL;
}
Node(int a, Node* n) {
data = a;
next = n;
}
~Node() {
delete(next);
}
void print() {
std::cout << data;
if (next == NULL) {
std::cout << std::endl;
} else {
std::cout << " ";
next->print();
}
}
};
Node* reverse(Node* list) {
Node* rev = NULL;
Node* cur = list;
while (cur != NULL) {
//create a new node
Node* head = new Node(cur->data, rev);
//add it to front of reverse list
rev = head;
//move forward on the original list
cur = cur->next;
}
return rev;
}
int main() {
Node* list = NULL;
for (int i=9; i>=0; i--) {
Node* head = new Node(i, list);
list = head;
}
std::cout << "the original list is ";
list->print();
Node* rev = reverse(list);
std::cout << "the reversed list is ";
rev->print();
delete(rev);
delete(list);
return 0;
}
Here is a sample output:
the original list is 0 1 2 3 4 5 6 7 8 9
the reversed list is 9 8 7 6 5 4 3 2 1 0
Hello, Give a Θ(n)-time nonrecursive procedure that reverses a singly linked list of n elements. The...
Implement an application based on a singly linked list that maintains a list of the top five performers in a video game. An entry on the list consists of a name and a score (you can make this into a blueprint class if you like), and the list must be maintained in descending order of scores. Here is an example of such a list when it only has three elements. Spike120 Whiz105 G-man 99 Use a class based on singly...
Answer all questions
1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...
Write a c++ routine that returns true if two elements in a singly linked list of integers add up to a given target (similar to the two sum problem 1). Your function should return true and print the individual numbers, or return false. Examples: - If your list is: 1 → 3 → 4 → 14 → 5 and your target is 8, then you will return true and print: 3, 5 (since 3 + 5 = 8). - If...
1.If a list is implemented as a singly linked stack, give the big-O worst-case time complexity of the following operations (as usual use the smallest standard big-O category that works: a) push_front, b) push_back, c) lookup, d) read the i'th member 2.Repeat question 3 for a dynamic array (for example, as in the C++ vector class)
ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in reverse order when you are NOT allowed to allocate memory dynamically. What is the running time of the algorithm? b) Write pseudo code to output a singly-linked list in reverse order when you are ALLOWED to allocate memory dynamically. What is the running time of the algorithm? c) You have an increasingly-sorted circular list (using an array) of n elements that is full. The...
Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...
IMPLEMENT AN IMMUTABLE SINGLY LINKED LIST IN JAVASCRIPT ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran. Please help me generate the singly linked list that will work and pass the tests. I will only use this as a reference so please add comments to explain what you are doing so I can understand and write this myself. I think a join method may need...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
// Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...
Question 2 (3 points) Given the following singly linked list (a list with four nodes), what will be the value stored at the last node when the following operations are executed? head->1111-151->11011->1211 Node curr = head; while(curr next !=null) { curr.item = curritem-head. item; curr = cur next; اسرة Node newNode = new Node(8): curr.next = newNode: 23 O 12 OS O21 Question 3 (3 points) Given a recursive method as shown below, what is the return value for P(5)...