"reverse linkedlist" Code Answer's

You're definitely familiar with the best coding language C that developers use to develop their projects and they get all their queries like "reverse linkedlist" answered properly. Developers are finding an appropriate answer about reverse linkedlist related to the C coding language. By visiting this online portal developers get answers concerning C codes question like reverse linkedlist. Enter your desired code related query in the search bar and get every piece of information about C code related question on reverse linkedlist. 

reverse a singly linked list in c

By Evil ElkEvil Elk on Sep 20, 2020
/**
 * C program to reverse a Singly Linked List
 */

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


/* Structure of a node */
struct node {
    int data; //Data part
    struct node *next; //Address part
}*head;


/* Functions used in the program */
void createList(int n);
void reverseList();
void displayList();


int main()
{
    int n, choice;

    /*
     * Create a singly linked list of n nodes
     */
    printf("Enter the total number of nodes: ");
    scanf("%d", &n);
    createList(n);

    printf("\nData in the list \n");
    displayList();

    /*
     * Reverse the list
     */
    printf("\nPress 1 to reverse the order of singly linked list\n");
    scanf("%d", &choice);
    if(choice == 1)
    {
        reverseList();
    }

    printf("\nData in the list\n");
    displayList();

    return 0;
}


/*
 * Create a list of n nodes
 */
void createList(int n)
{
    struct node *newNode, *temp;
    int data, i;

    if(n <= 0)
    {
        printf("List size must be greater than zero.\n");
        return;
    }

    head = (struct node *)malloc(sizeof(struct node));

    /*
     * If unable to allocate memory for head node
     */
    if(head == NULL)
    {
        printf("Unable to allocate memory.");
    }
    else
    {
        /*
         * Read data of node from the user
         */
        printf("Enter the data of node 1: ");
        scanf("%d", &data);

        head->data = data; // Link the data field with data
        head->next = NULL; // Link the address field to NULL

        temp = head;

        /*
         * Create n nodes and adds to linked list
         */
        for(i=2; i<=n; i++)
        {
            newNode = (struct node *)malloc(sizeof(struct node));

            /* If memory is not allocated for newNode */
            if(newNode == NULL)
            {
                printf("Unable to allocate memory.");
                break;
            }
            else
            {
                printf("Enter the data of node %d: ", i);
                scanf("%d", &data);

                newNode->data = data; // Link the data field of newNode with data
                newNode->next = NULL; // Link the address field of newNode with NULL

                temp->next = newNode; // Link previous node i.e. temp to the newNode
                temp = temp->next;
            }
        }

        printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
    }
}


/*
 * Reverse the order of nodes of a singly linked list
 */
void reverseList()
{
    struct node *prevNode, *curNode;

    if(head != NULL)
    {
        prevNode = head;
        curNode = head->next;
        head = head->next;

        prevNode->next = NULL; // Make first node as last node

        while(head != NULL)
        {
            head = head->next;
            curNode->next = prevNode;

            prevNode = curNode;
            curNode = head;
        }

        head = prevNode; // Make last node as head

        printf("SUCCESSFULLY REVERSED LIST\n");
    }
}


/*
 * Display entire list
 */
void displayList()
{
    struct node *temp;

    /*
     * If the list is empty i.e. head = NULL
     */
    if(head == NULL)
    {
        printf("List is empty.");
    }
    else
    {
        temp = head;
        while(temp != NULL)
        {
            printf("Data = %d\n", temp->data); // Print the data of current node
            temp = temp->next;                 // Move to next node
        }
    }
}

Source: codeforwin.org

Add Comment

2

revese the linked list java

By Inquisitive IguanaInquisitive Iguana on Aug 05, 2020
Easiest way


public static LinkedList reverse(LinkedList head) {
		LinkedList prevAddress = null;
		LinkedList currentAddress = head;
		LinkedList nextAddress = head.next;
		
		while(nextAddress!=null) {
			currentAddress.next = prevAddress;
			prevAddress = currentAddress;
			currentAddress = nextAddress;
			nextAddress= currentAddress.next;
			currentAddress.next = prevAddress;
		}
		head = currentAddress;
		return head;
	}
    
Just try to visualize its all about pointers game. :-)
  NO GFG ANSWER

Add Comment

1

reverse a linked list

By SaanSaan on Sep 08, 2020
class recursion { 
	static Node head; // head of list 
	static class Node { 
		int data; 
		Node next; 
		Node(int d) 
		{   data = d; 
			next = null; 	} } 
	static Node reverse(Node head) 
	{ 
		if (head == null || head.next == null) 
			return head; 
		/* reverse the rest list and put the first element 
        at the end */
		Node rest = reverse(head.next); 
		head.next.next = head; 
		/* tricky step -- see the diagram */
    	head.next = null; 
		/* fix the head pointer */
		return rest; 
	}  /* Function to print linked list */
	static void print() 
	{ 
		Node temp = head; 
		while (temp != null) { 
			System.out.print(temp.data + " "); 
			temp = temp.next; 
		} 
		System.out.println(); 
	} 
	static void push(int data) 
	{ 
		Node temp = new Node(data); 
		temp.next = head; 
		head = temp; 
	} /* Driver program to test above function*/
public static void main(String args[]) 
{ 
	/* Start with the empty list */
	push(20); 
	push(4); 
	push(15); 
	push(85); 
	System.out.println("Given linked list"); 
	print(); 
	head = reverse(head); 
	System.out.println("Reversed Linked list"); 
	print(); 
} } // This code is contributed by Prakhar Agarwal 

Add Comment

4

reverse linkedlist

By Wandering WolverineWandering Wolverine on May 13, 2021
Collections.reverse(list);

Source: stackoverflow.com

Add Comment

0

All those coders who are working on the C based application and are stuck on reverse linkedlist can get a collection of related answers to their query. Programmers need to enter their query on reverse linkedlist related to C code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about reverse linkedlist for the programmers working on C code while coding their module. Coders are also allowed to rectify already present answers of reverse linkedlist while working on the C language code. Developers can add up suggestions if they deem fit any other answer relating to "reverse linkedlist". Visit this developer's friendly online web community, CodeProZone, and get your queries like reverse linkedlist resolved professionally and stay updated to the latest C updates. 

C answers related to "reverse linkedlist"

View All C queries

C queries related to "reverse linkedlist"

Browse Other Code Languages

CodeProZone