Reversing a linked list using 2 pointers [closed]












0















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?










share|improve this 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
















0















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?










share|improve this 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














0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












2 Answers
2






active

oldest

votes


















1














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;
}





share|improve this answer

































    0














    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;
    }





    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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;
      }





      share|improve this answer






























        1














        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;
        }





        share|improve this answer




























          1












          1








          1







          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;
          }





          share|improve this answer















          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;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 12:43

























          answered Nov 16 '18 at 12:36









          snake_stylesnake_style

          1,170410




          1,170410

























              0














              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;
              }





              share|improve this answer




























                0














                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;
                }





                share|improve this answer


























                  0












                  0








                  0







                  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;
                  }





                  share|improve this answer













                  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;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 13:02









                  FlairFlair

                  154




                  154















                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()