when inputting the LL my last element is not getting inserted can you please debug that . And also can you suggest an approach to this problem
Doubt in code and logic
Hey @chitranshanmol07
Please share your code in Coding Blocks IDE
Approach:
Take two pointers pointed to head of list
Now move one of them k ahead
Now move both together one node at a time
When secon pointer reaches null then first node is pointing to kth node from the back.
Hey @chitranshanmol07
Problem is in your print function :
void print(node*head){
while(head->next!=NULL){
cout<data<<" ";
head=head->next;
}
}
Here condition in while should be (head!=NULL)
Otherwise, Say we have only one element in LL then its next is already NULL so your loop will not trigger.