patterncppMinor
Linked list order reverse program efficiency
Viewed 0 times
orderreverseprogramefficiencylistlinked
Problem
Here is my function which convert linked list in the following order:
Example:
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
Output: 3->2->1->6->5->4->8->7->NULL.
This is my number of elements in the linked list:
Please help me to optimize this code. If you require full code then let me know and I will add more. But I guess this is the
Does it depend on our laptop configuration and process as well?
Full source:
```
#pragma once
#include
#include
struct
Example:
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
Output: 3->2->1->6->5->4->8->7->NULL.
void SingleList::orderSort3()
{
// If linked list is empty or there is only one node or two nodes in list
if(getHead() == NULL || getHead()->getNext() == NULL || getHead()->getNext()->getNext() == NULL)
return;
// Initialize previous and current pointers
ISingleNode *prev = getHead();
ISingleNode *current = getHead()->getNext()->getNext();
// Change head before proceeeding, because this head will be useful at last.
setHead(current);
// Traverse the list and swap
while(true)
{
ISingleNode *tempNext = current->getNext();
current->setNext(prev->getNext());
prev->getNext()->setNext(prev);
// check remaining node is not empty or single node.
if(tempNext == NULL || tempNext->getNext() == NULL)
{
prev->setNext(tempNext);
break;
}
// check remaining nodes are not only 2.
if(tempNext->getNext()->getNext() == NULL)
{
prev->setNext(tempNext->getNext());
prev = tempNext;
current = tempNext->getNext();
}
else // 3 nodes are available.
{
prev->setNext(tempNext->getNext()->getNext());
prev = tempNext;
current = tempNext->getNext()->getNext();
}
}
}This is my number of elements in the linked list:
8388607
14348907Please help me to optimize this code. If you require full code then let me know and I will add more. But I guess this is the
main() function, which reverses the linked list. It takes only a head pointer as an argument and it will sort without returning. This is what I want, but it takes too much time. Does it depend on our laptop configuration and process as well?
Full source:
```
#pragma once
#include
#include
struct
Solution
-
This is a little unclear:
Although you have stated the value of
Then just put that into the loop:
This will also make it a little easier to change this value.
-
It's actually uncommon for a linked list to have getters and setters. Also, the specific node type is not supposed to be exposed to the client code, which would be the case for getters and setters. Plus, if you're just trying to reverse the linked list, then the client shouldn't need this.
This can be changed by removing these functions (or at least making them
-
Since you're using C++11, you should use
This is a little unclear:
for(int i = 8388607 - 1; i >= 0; --i)Although you have stated the value of
8388607, you should preferably make it a constant in the code as well:const int values = 8388607;Then just put that into the loop:
for (int i = values - 1; i >= 0; --i)This will also make it a little easier to change this value.
-
It's actually uncommon for a linked list to have getters and setters. Also, the specific node type is not supposed to be exposed to the client code, which would be the case for getters and setters. Plus, if you're just trying to reverse the linked list, then the client shouldn't need this.
This can be changed by removing these functions (or at least making them
private) and putting the ISingleNode implementation into SingleList, also as private.-
Since you're using C++11, you should use
nullptr instead of NULL.Code Snippets
for(int i = 8388607 - 1; i >= 0; --i)const int values = 8388607;for (int i = values - 1; i >= 0; --i)Context
StackExchange Code Review Q#80317, answer score: 2
Revisions (0)
No revisions yet.