Write a program to reverse a linked list

This question is related to Aztec-Systems Interview

Showing Answers 1 - 7 of 7 Answers

chandan

  • Jul 18th, 2006
 

Code
  1. span style="color: #808080; font-style: italic;">/* AFTER CREATING LINK LIST *//* insert value of 1st node  into p */

  2.         h = h->next;

  3.         p->next = H;            /* insert p->next =NULL ,because H is equals to NULL (1st time) */

  4.         H = p;                  /* insert address of p into H */

  Was this answer useful?  Yes

pranjal5215

  • Jul 10th, 2009
 

This is the recursive version of the program.



Code
  1. #include<stdio.h>

  2. #include<stdlib.h>

  3. " %d", temp->month);

  4. }

  5.  

  6. node *reverse_recurse(node * cur, node * start)

  7. {                               /*reverse linked list recursively */"n"

  Was this answer useful?  Yes

cleonjoys

  • Oct 21st, 2010
 

This is dummy program I have written concentrating mainly on reversing the List rather than implementing efficient way in adding to list and displaying it :)

I have written Algo for the reverse also please check it for easier understanding.



Code
  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. /* Design flow

  4. .

  5. 1    rev(2) -> store NULL

  6.     2    rev(3) -> store 1

  7.         3    NULL -> store 2

  8.             ..continues after the recursive calls, so here it needs to store the previous

  9.             value

  10.    

  11. call is

  12.     reverse(start, NULL)

  13.  

  14. 1->2->3

  15. call reverse(1, NULL)

  16.         nptr = 1

  17.         prevPtr = NULL

  18.         1->next != NULL

  19.             call reverse(2, 1)

  20.             nptr = 2

  21.             prevPtr = 1

  22.             2->next != NULL

  23.                 call reverse(3, 2)

  24.                 nptr = 3

  25.                 prevPtr = 2

  26.                 3->next == NULL; so return from here

  27.    

  28.                 3->next = 2

  29.             2->next = 1

  30.         1->next = NULL

  31. */

  32.  

  33. /* Its O(n-1) in space complexity */"System OOOn""%d ""%dn""Starting creation of the List!!n""Contents of the list as follows:-n""After reverse the list has: n"

  Was this answer useful?  Yes

ss0101782

  • Dec 31st, 2010
 

Code
  1. #include"stdio.h"

  2. #include"stdlib.h"

  3. #include"conio.h"

  4. #include"string.h"

  5. #include"alloc.h"

  6. "n ********** Add node at begining of the list **********""n ********** Add node at end of the list **********""n ********** Add node at any position of the list **********""n ********** Deletion of node from the list **********""n ********** Revers the list **********""n Enter the number of nodes::");

  7.     scanf("%d""n the node number::%d""n Enter student roll number::");

  8.         scanf("%d""n Enter student name::""n Enter student total marks::");

  9.         scanf("%d""n student roll number::%4d""n student name ::%s""n student total marks::%4d""n--------------------------------""n Enter student roll number::");

  10.     scanf("%d""n Enter student name::""n Enter student total marks::");

  11.     scanf("%d""n Enter student roll number::");

  12.     scanf("%d""n Enter student name::""n Enter student total marks::");

  13.     scanf("%d""n Enter the position where you want to add the node::");

  14.     scanf("%d""n wrong entry::""n Enter student roll number::");

  15.     scanf("%d""n Enter student name::""n Enter student total marks::");

  16.     scanf("%d""n sorry position not found::""n Enter student's roll number which is to be deleted::");

  17.     scanf("%d", &rl);

  18.     fflush(stdin);

  19.     p1 = tail;

  20.     // p=head1;"n node position not found::"

  Was this answer useful?  Yes

ankit srivastava

  • Oct 17th, 2011
 

public void ReverseLinkedList (LinkedList linkedList)
{
// ------------------------------------------------------------
// Create a new linked list and add all items of given
// linked list to the copy linked list in reverse order
// ------------------------------------------------------------

LinkedList copyList = new LinkedList();

// ------------------------------------------------------------
// Start from the latest node
// ------------------------------------------------------------

LinkedListNode start = linkedList.Tail;

// ------------------------------------------------------------
// Traverse until the first node is found
// ------------------------------------------------------------

while (start != null)
{
// ------------------------------------------------------------
// Add item to the new link list
// ------------------------------------------------------------

copyList.Add (start.Item);

start = start.Previous;
}

linkedList = copyList;

// ------------------------------------------------------------
// That's it!
// ------------------------------------------------------------
}

  Was this answer useful?  Yes

samCPP

  • Dec 4th, 2011
 

Reversing a Linked list using temporary pointer


Code
  1. #include<iostream.h>

  2. #include<conio.h>

  3. "   "//Temporary pointer for reversal"    ""Enter choice""enter element "" List is "" Reverse List is "

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions