Okay, so I have been doing competitive programming for the past few days in C++. And day before yesterday, I stumbled upon this concept of linked list.
Now, what I do while implementing most problems pertaining to linked list is that I store all the values of the linked list nodes in a vector, and do operations on them which has been asked as per the question, then refill the nodes with the newly updated values of the vector. Now, most of the questions can be done by me really easily, as I am pretty comfortable with using vectors now. I just wanted to ask this community if this was a bad approach towards using linked list, and should I stop using it altogether.
I will give an example to sort a linked list in $O(N\log N)$ time, which I used. Following is a function that sorts a linked list-
ListNode* Solution::sortList(ListNode* A) {
vector<int> vect;
ListNode* temp=A;
while(A!=NULL)
{
vect.push_back(A->val);
A=A->next;
}
A=temp;
sort(vect.begin(), vect.end());
for(int i=0; i<vect.size(); i++)
{
A->val=vect[i];
A=A->next;
}
return temp;
}