Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:27:34+00:00 2026-06-17T05:27:34+00:00

I have a linked list, now I want to walk through it and each

  • 0

I have a linked list, now I want to walk through it and each time I advance in it, I move the current node to the beginning of list like this:

4 5 3 2 7
5 4 3 2 7
3 5 4 2 7
2 3 5 4 7
7 2 3 5 4

The code:

#include <stdio.h>
#include <stdlib.h>

typedef struct _node {
    int p;
    struct _node *next;
} node;

main(int argc, char **argv)
{
    int i, n;
    node *nod = NULL;
    node *nod2 = NULL;
    node *nod_tmp = NULL;

    node *nod_next = NULL;
    node *nod_before = NULL;

    printf("Enter n: ");
    scanf("%d", &n);
    for(i = 0; i < n; ++i)
    {
        nod_tmp = (node *)malloc(sizeof(node));
        scanf("%d", &nod_tmp->p);
        if (i == 0)
        {
            nod = nod2 = nod_tmp;
        }
        nod2->next = nod_tmp;
        nod2 = nod_tmp;
    }

    nod_tmp = nod;
    while (nod_tmp->next != NULL)
    {
        nod_before = nod_tmp; // save current node
        nod_next = nod_tmp->next->next; // save next node
        nod_before->next = nod_next; // link the previous with the next one
        nod_tmp->next = nod; // point the current node to the beginning of list
    }

    while(nod != NULL)
    {   
        printf("%d\n", nod->p);
        nod = nod->next;
    }
    return 0;
}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:27:35+00:00Added an answer on June 17, 2026 at 5:27 am
    nod_tmp = nod->next;
    nod_before= nod;
    while (nod_tmp != NULL)
    {
         nod_before->next = nod_tmp->next;
         nod_tmp->next = nod;
         nod = nod_tmp;
         nod_tmp = nod_before->next;
    }
    

    Change your second loop to this loop above. Note that node_tmp points to the second node after the head and nod_before points to the first node. This way you avoid infinite loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made my own single chained/linked list. Now, if I want to delete/remove
I have written a list-like template class sll (Single Linked List). Now, I am
I have a Linked List and I want to implement a function: Random_Shuffle_List (struct
I have a singly-linked list containing 1 value in each element. struct listElement {
I have a singly linked list with node and list structs defined as: typedef
I have a vector of Linked list pointers. Each LinkedList has a head pointer
Suppose I have a doubly-linked list that has a pointer to the first node
I have a generic linked list implementation with a node struct containing a void*
I am new to linked list, now I have little problems in population of
I want to created a generic sorted linked list. Thus I have an abstract

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.