Programming Geek
Rated 4.7/5 based on 1446 reviews Reversing a linked list means changing the order of the node i. e. the first node becomes the last node and the last node becomes the first node.  A linked list can be reversed using three variable called pre(previous node pointer), cur (current node pointer) and next(next node pointer) and performing following operations while next is not null:

1) make cur link part to point to pre.
2)make pre to point to pre.
3) make next to point to next node.
4)assign next to cur as this will be current node in next iteration.

This is illustrated in following C program:

``````//reversing  a linked list
/**
*
*@author VIKASH VIK VIKASHVVERMA
*
*/

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

typedef struct node
{
int data;
struct node*next;
}node;

{
node*temp=(node*)malloc(sizeof(node));
temp->data=num;
temp->next=*s;
*s=temp;
}

void traverse(node*s)
{
while(s!=NULL)
{
printf("%d\t",s->data);
s=s->next;
}
}
void reverse(node**s)
{
node*pre=NULL,*cur=NULL,*next=NULL;
cur=*s;
next=*s;
while(next!=NULL)
{

next=cur->next;
cur->next=pre;
pre=cur;
cur=next;

}
*s=pre;
}

void main()
{
node*n=NULL;
{"ft_lang_label":"__label__en","ft_lang_prob":0.6783812,"math_prob":0.75585103,"size":1458,"snap":"2023-40-2023-50","text_gpt3_token_len":411,"char_repetition_ratio":0.14580467,"word_repetition_ratio":0.3611111,"special_character_ratio":0.29903978,"punctuation_ratio":0.17325228,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9872909,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-26T01:26:01Z\",\"WARC-Record-ID\":\"<urn:uuid:e945a208-4403-47f6-87a4-9037272dbe0d>\",\"Content-Length\":\"88891\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d5b48821-5414-4c80-82a7-9039c275f55f>\",\"WARC-Concurrent-To\":\"<urn:uuid:2aba0c62-22ff-464a-879a-d335321f4db5>\",\"WARC-IP-Address\":\"142.251.16.121\",\"WARC-Target-URI\":\"https://www.programminggeek.in/2013/06/reversing-a-linked-list-in-c.html\",\"WARC-Payload-Digest\":\"sha1:XYDNLD3MWT56HKNA33C4WNKXJWLNY6E7\",\"WARC-Block-Digest\":\"sha1:EVMV5OM624P367F2Z7Y7HCR63ZSXKE4L\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510130.53_warc_CC-MAIN-20230926011608-20230926041608-00353.warc.gz\"}"}