Reversing a linked list using 2 pointers [closed]
I wrote a code to reverse a linked list using two pointers. At a time, I am reversing 1 pointer between 2 nodes. The code looks fine to me.
Node* reverse(Node *head)
{
Node *p = head;
Node *q = p->next;
Node *temp;
while (q != NULL) {
temp = p;
p = q->next;
q->next = temp;
temp = p;
p=q;
q=temp;
}
head = p;
return p;
}
What is the logical fault here?
c++ linked-list
closed as off-topic by Fantastic Mr Fox, Some programmer dude, gsamaras, Peter Ruderman, Baum mit Augen Nov 25 '18 at 21:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Fantastic Mr Fox, gsamaras, Peter Ruderman, Baum mit Augen
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I wrote a code to reverse a linked list using two pointers. At a time, I am reversing 1 pointer between 2 nodes. The code looks fine to me.
Node* reverse(Node *head)
{
Node *p = head;
Node *q = p->next;
Node *temp;
while (q != NULL) {
temp = p;
p = q->next;
q->next = temp;
temp = p;
p=q;
q=temp;
}
head = p;
return p;
}
What is the logical fault here?
c++ linked-list
closed as off-topic by Fantastic Mr Fox, Some programmer dude, gsamaras, Peter Ruderman, Baum mit Augen Nov 25 '18 at 21:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Fantastic Mr Fox, gsamaras, Peter Ruderman, Baum mit Augen
If this question can be reworded to fit the rules in the help center, please edit the question.
4
Please take some time to review how to ask good questions, as well as this question checklist. Lastly don't forget how to create a Minimal, Complete, and Verifiable Example.
– Some programmer dude
Nov 16 '18 at 12:20
You are swapping the elements side by side, which will effectively just move the head to the end of the list. You need to be swapping the start and the end element then moving towards the middle of the linked list.
– Fantastic Mr Fox
Nov 16 '18 at 12:22
@FantasticMrFox I am swapping the links between the elements side by side, and then making the head point to last element. So effectively the linked list is reversed. 10->20->30->40->50 to 10<-20<-30<-40<-50
– Flair
Nov 16 '18 at 12:28
add a comment |
I wrote a code to reverse a linked list using two pointers. At a time, I am reversing 1 pointer between 2 nodes. The code looks fine to me.
Node* reverse(Node *head)
{
Node *p = head;
Node *q = p->next;
Node *temp;
while (q != NULL) {
temp = p;
p = q->next;
q->next = temp;
temp = p;
p=q;
q=temp;
}
head = p;
return p;
}
What is the logical fault here?
c++ linked-list
I wrote a code to reverse a linked list using two pointers. At a time, I am reversing 1 pointer between 2 nodes. The code looks fine to me.
Node* reverse(Node *head)
{
Node *p = head;
Node *q = p->next;
Node *temp;
while (q != NULL) {
temp = p;
p = q->next;
q->next = temp;
temp = p;
p=q;
q=temp;
}
head = p;
return p;
}
What is the logical fault here?
c++ linked-list
c++ linked-list
asked Nov 16 '18 at 12:18
FlairFlair
154
154
closed as off-topic by Fantastic Mr Fox, Some programmer dude, gsamaras, Peter Ruderman, Baum mit Augen Nov 25 '18 at 21:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Fantastic Mr Fox, gsamaras, Peter Ruderman, Baum mit Augen
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Fantastic Mr Fox, Some programmer dude, gsamaras, Peter Ruderman, Baum mit Augen Nov 25 '18 at 21:44
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Fantastic Mr Fox, gsamaras, Peter Ruderman, Baum mit Augen
If this question can be reworded to fit the rules in the help center, please edit the question.
4
Please take some time to review how to ask good questions, as well as this question checklist. Lastly don't forget how to create a Minimal, Complete, and Verifiable Example.
– Some programmer dude
Nov 16 '18 at 12:20
You are swapping the elements side by side, which will effectively just move the head to the end of the list. You need to be swapping the start and the end element then moving towards the middle of the linked list.
– Fantastic Mr Fox
Nov 16 '18 at 12:22
@FantasticMrFox I am swapping the links between the elements side by side, and then making the head point to last element. So effectively the linked list is reversed. 10->20->30->40->50 to 10<-20<-30<-40<-50
– Flair
Nov 16 '18 at 12:28
add a comment |
4
Please take some time to review how to ask good questions, as well as this question checklist. Lastly don't forget how to create a Minimal, Complete, and Verifiable Example.
– Some programmer dude
Nov 16 '18 at 12:20
You are swapping the elements side by side, which will effectively just move the head to the end of the list. You need to be swapping the start and the end element then moving towards the middle of the linked list.
– Fantastic Mr Fox
Nov 16 '18 at 12:22
@FantasticMrFox I am swapping the links between the elements side by side, and then making the head point to last element. So effectively the linked list is reversed. 10->20->30->40->50 to 10<-20<-30<-40<-50
– Flair
Nov 16 '18 at 12:28
4
4
Please take some time to review how to ask good questions, as well as this question checklist. Lastly don't forget how to create a Minimal, Complete, and Verifiable Example.
– Some programmer dude
Nov 16 '18 at 12:20
Please take some time to review how to ask good questions, as well as this question checklist. Lastly don't forget how to create a Minimal, Complete, and Verifiable Example.
– Some programmer dude
Nov 16 '18 at 12:20
You are swapping the elements side by side, which will effectively just move the head to the end of the list. You need to be swapping the start and the end element then moving towards the middle of the linked list.
– Fantastic Mr Fox
Nov 16 '18 at 12:22
You are swapping the elements side by side, which will effectively just move the head to the end of the list. You need to be swapping the start and the end element then moving towards the middle of the linked list.
– Fantastic Mr Fox
Nov 16 '18 at 12:22
@FantasticMrFox I am swapping the links between the elements side by side, and then making the head point to last element. So effectively the linked list is reversed. 10->20->30->40->50 to 10<-20<-30<-40<-50
– Flair
Nov 16 '18 at 12:28
@FantasticMrFox I am swapping the links between the elements side by side, and then making the head point to last element. So effectively the linked list is reversed. 10->20->30->40->50 to 10<-20<-30<-40<-50
– Flair
Nov 16 '18 at 12:28
add a comment |
2 Answers
2
active
oldest
votes
Node* reverse(Node *head)
{
if(!head)
return nullptr;
Node *p = head;
Node *q = p->next;
p->next = nullptr;
while(q != NULL)
{
Node *temp = q->next;
q->next = p;
p = q;
q = temp;
}
head = p;
return p;
}
add a comment |
What I missed was that I didn't make the next of first node of the original linked list point to NULL. That's why it was going into infinite loop.
Node* reverse(Node *head)
{
Node *p = head;
Node *q = p->next;
Node *temp;
p->next=NULL;
while (q != NULL) {
temp = p;
p = q->next;
q->next = temp;
temp = p;
p=q;
q=temp;
}
head = p;
return p;
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Node* reverse(Node *head)
{
if(!head)
return nullptr;
Node *p = head;
Node *q = p->next;
p->next = nullptr;
while(q != NULL)
{
Node *temp = q->next;
q->next = p;
p = q;
q = temp;
}
head = p;
return p;
}
add a comment |
Node* reverse(Node *head)
{
if(!head)
return nullptr;
Node *p = head;
Node *q = p->next;
p->next = nullptr;
while(q != NULL)
{
Node *temp = q->next;
q->next = p;
p = q;
q = temp;
}
head = p;
return p;
}
add a comment |
Node* reverse(Node *head)
{
if(!head)
return nullptr;
Node *p = head;
Node *q = p->next;
p->next = nullptr;
while(q != NULL)
{
Node *temp = q->next;
q->next = p;
p = q;
q = temp;
}
head = p;
return p;
}
Node* reverse(Node *head)
{
if(!head)
return nullptr;
Node *p = head;
Node *q = p->next;
p->next = nullptr;
while(q != NULL)
{
Node *temp = q->next;
q->next = p;
p = q;
q = temp;
}
head = p;
return p;
}
edited Nov 16 '18 at 12:43
answered Nov 16 '18 at 12:36
snake_stylesnake_style
1,170410
1,170410
add a comment |
add a comment |
What I missed was that I didn't make the next of first node of the original linked list point to NULL. That's why it was going into infinite loop.
Node* reverse(Node *head)
{
Node *p = head;
Node *q = p->next;
Node *temp;
p->next=NULL;
while (q != NULL) {
temp = p;
p = q->next;
q->next = temp;
temp = p;
p=q;
q=temp;
}
head = p;
return p;
}
add a comment |
What I missed was that I didn't make the next of first node of the original linked list point to NULL. That's why it was going into infinite loop.
Node* reverse(Node *head)
{
Node *p = head;
Node *q = p->next;
Node *temp;
p->next=NULL;
while (q != NULL) {
temp = p;
p = q->next;
q->next = temp;
temp = p;
p=q;
q=temp;
}
head = p;
return p;
}
add a comment |
What I missed was that I didn't make the next of first node of the original linked list point to NULL. That's why it was going into infinite loop.
Node* reverse(Node *head)
{
Node *p = head;
Node *q = p->next;
Node *temp;
p->next=NULL;
while (q != NULL) {
temp = p;
p = q->next;
q->next = temp;
temp = p;
p=q;
q=temp;
}
head = p;
return p;
}
What I missed was that I didn't make the next of first node of the original linked list point to NULL. That's why it was going into infinite loop.
Node* reverse(Node *head)
{
Node *p = head;
Node *q = p->next;
Node *temp;
p->next=NULL;
while (q != NULL) {
temp = p;
p = q->next;
q->next = temp;
temp = p;
p=q;
q=temp;
}
head = p;
return p;
}
answered Nov 16 '18 at 13:02
FlairFlair
154
154
add a comment |
add a comment |
4
Please take some time to review how to ask good questions, as well as this question checklist. Lastly don't forget how to create a Minimal, Complete, and Verifiable Example.
– Some programmer dude
Nov 16 '18 at 12:20
You are swapping the elements side by side, which will effectively just move the head to the end of the list. You need to be swapping the start and the end element then moving towards the middle of the linked list.
– Fantastic Mr Fox
Nov 16 '18 at 12:22
@FantasticMrFox I am swapping the links between the elements side by side, and then making the head point to last element. So effectively the linked list is reversed. 10->20->30->40->50 to 10<-20<-30<-40<-50
– Flair
Nov 16 '18 at 12:28