Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Reverse a linked list geeksforgeeks in c++

/* 
   problem link: https://practice.geeksforgeeks.org/problems/reverse-a-linked-list/1/?page=1&difficulty[]=0&status[]=unsolved&sortBy=submissions#
*/
///////////////////////////////////////////////////////////////////////////////////////////
class Solution
{
    public:
    //Function to reverse a linked list.
    struct Node* reverseList(struct Node *head)
    {
        // code here
        // return head of reversed list
        Node *p, *c, *n;
        p=NULL;
        c=head;
        while(c!=NULL)
        {
            n=c->next;
            c->next=p;
            p=c;
            c=n;
        }
        head=p;
        return head;
    }
    
};
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #Reverse #linked #list #geeksforgeeks
ADD COMMENT
Topic
Name
2+1 =