Reverse a singly linked list – iterative program

♠ Posted by GeekyFry in ,,,,,, at 6:22 AM

Write an iterative function to reverse a given single linked-list
 
Possible Solution:
C function:
1
2
3
4
5
6
7
8
9
10
11
12
Node * reverse(Node * t)
 
{
  Node * prev = NULL, *curr = t, *next;
  while(curr) 
  {
    next = curr->next;
    curr->next = prev;
    curr = prev;
  }
  return prev;
}

0 comments:

Post a Comment