35103547926423517248 Putting this linked list through the fu
35->10->35->47->9->26->42->35->17->24->8->
Putting this linked list through the function below it takes the last half of the list and puts it in front. I understand that, but I dont understand the line of code that i commented on. what does (last->next = first) do in the function??
Void SLL::sj() {
SNode *tmp = first;
for(int i = 0; i < size/2; i++) {
tmp = tmp->next;
}
last->next = first //What does this do in the function?? does it set 8 equal to first, which would be 35\'s previous??
first = tmp->next;
tmp->next = NULL;
}
Solution
Void SLL::sj() {
SNode *tmp = first; // first is storing the pointer to the first element ie 35
for(int i = 0; i < size/2; i++) { // size =11 ,size/2= 5, i=0 to 4, 5 elements are traversed, 10,35,47,9,26
tmp = tmp->next; // temp =26
}
//last is storing the pointer to the last element in list ie 8 .Now last->next = null and first =35
last->next = first // by making last->next =first , the element after 8 will be 35 in the list as first is 35
first = tmp->next; // value in temp->next = 26->next = 42, so first element in the new list is 42
tmp->next = NULL; // 26->next = null so 26 will be the last element in the list
}
