/**
* 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) {
if(head==NULL || head->next==NULL)
return head;
ListNode *p=NULL;
ListNode *q=NULL;
ListNode *r=NULL;
r=head;
q=head->next;
if(q->next){
p=q->next;
}else{
q->next=r;
return q;
}
while(p){
q->next=r;
r=q;
q=p;
p=p->next;
}
head->next=NULL;
return q;
}
};
I know this is one of the most basic problems of linked list but I tried it on paper and then coded it but I am getting errors for don't know what reasons.Could somebody please point out the error in my logic and how to correct it?
P.s: Don't give me solutions rather help in finding errors in my code
ListNode *r=NULL; r=head;should beListNode *r=head;. Don't waste time initializing an object with a value that you're going to immediately overwrite.mainfunction that feeds in an input that caused the problem you are asking about.dsa, for example, is used with questions about the Digital Signature Algorithm, something noticeably absent from this question.