GeeksforGeeks » Linked List specific questions
LINK LIST
(3 posts)-
EFFICIENT METHOD OF REVERSE A LINK LIST USING SINGLE POINTER ONLY , NO EXTRA POINTER IS USED
-
@AMIT, Suppose we have a circularly singly linked list, then in O(N) time complexity we can reverse your list circulary linked list.
frankchang91@gmail.comstruct znode{
int value;
struct znode* next;};
znode* h79(znode*& node ,znode* start){
znode* retval;
if (node->next == start)
return node;
retval = h79(node->next,start);
if (node->next){
node->next->next = node;
node->next = retval;
}
return retval;
}//test program
znode* SLNode3;
znode* SLNode5;
znode* SLNode4;
znode* result;SLNode3 = new znode;
SLNode3->value = -5;
SLNode3->next = NULL;SLNode5 = new znode;
SLNode5->value = -3;
SLNode5->next = SLNode3;SLNode4 = new znode;
SLNode4->value = 6;
SLNode4->next = SLNode5;znode* temporary = SLNode3->next;
SLNode3->next = SLNode4;
temporary = NULL;result = h79(SLNode4, SLNode4);
-
@AMIT if we change the circularly singly linked list to an ordinary singly list , then we have a simpler solution.
struct znode{
int value;
struct znode* next;};
znode* h79(znode*& node){
znode* retval;
if (node && node->next == NULL)
return node;
retval = h79(node->next);
if (node->next){
node->next->next = node;
node->next = NULL;
}
return retval;
}
Reply
You must log in to post.