Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Consider a singly linked list.

First Node : value=v1,address of this node=x,next=y ;

Second Node : value=v2,address of this node=y,next=NULL

Linked list pointer *ptr=x;
Linked list pointer *ptr1=y;
ptr1->next=ptr; // This is wrong

This will show the error of "heap-use-after-free"
WHY I can't store the address of 2 nodes in each other?

ptr->next=NULL // or any other node address
ptr1->next=ptr; // This is right

I can use this but why I can not use the above one?

(I don't need to traverse to the end and you can assume there are a lot of nodes)

What I have tried:

CODE FOR BETTER UNDERSTANDABILITY OF QUESTION
This is showing error why?
C++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
     ListNode* reverseList(ListNode* head) {
        ListNode *a,*b,*c;
        a=head;
        if(!a)
            return a;
        a=head->next;
        b=head->next->next;
        while(b!=NULL)
        {
            c=b->next;
            b->next=a;
            a=b;
            b=c;
        }
        head->next=NULL;
        return a;
    }

now below one is correct
C++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *a,*b,*c;
        a=head;
        if(!a)
            return a;
        b=head->next;
        head->next=NULL;
        while(b!=NULL)
        {
            c=b->next;
            b->next=a;
            a=b;
            b=c;
        }
        return a;
    }
};
Posted
Updated 26-Jan-21 4:13am
v4

Think about it. Say you need to enumerate the list and display all of the objects or do some calculation on each object in the list. Start at the beginning of the list and enumerate the list, do your work on each object and follow the Next pointer to the next object in the list. Keep going until you get to the end of the list, there's no more Next pointers to follow. Your work is done!

Oh wait, that never happens! Since you now have two nodes where the Next pointers point at each other, you will never get to the end of the list and terminate the enumeration. The enumeration will go on forever because you never find a Next pointer that is null.

Creating a ring in your list defeats the purpose of it being a "list".
 
Share this answer
 
Comments
Member 15056063 26-Jan-21 4:49am    
I am not traversing both sides.
I am just not doing head->next=NULL in the beginning.
Dave Kreskowiak 26-Jan-21 10:41am    
Both sides? What are you talking about? I never said anything about "sides". A singly linked list just has a single pointer to the next node in the list.

I told you why you can NOT have two nodes that point to each other. There is no reason to do that in a singly-linked list.

A singly linked list should looks like this:
Node1
-----
data
Next ---> Node2
          -----
          data
          Next ---> Node3
                    -----
                    data
                    Next ---> null
Member 15056063 26-Jan-21 22:49pm    
Ohh, now it's clear. Thanks for your help, Sir.😊
To add to what Dave Kreskowiak has said: yes, technically you can do that. The systems does not in any way prevent you from pointing pointers anywhere you like. You can even make a node self referential:
C++
ListNode* ReallyMessItUp(ListNode* head) 
   {
   head->next = head;
   }

But just because you can do something, doesn't mean you should.
Most developers go to great lengths to prevent loops happening - regardless of how long or short they are - because they can trap code in a never-ending loop.
The only exception to this is when when a linked list is being used as a Queue, when you need the ability to add at one point and remove from teh other, and teh two may not be synchronous (I might add a node every second, and remove at a rate that varies between 0.5 seconds to 3 seconds per node, for example).
 
Share this answer
 
Comments
Member 15056063 26-Jan-21 22:54pm    
Thanks for the support, Sir.
I will remember this "you can do something, doesn't mean you should". This gonna save me a lot!😅😄

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900