title: Reversing a linked list in a specific manner through PHP
body: <p>Given a <strong>Singly Linked List</strong> $link, with elements (a->b->c->d->e->f->g->h->i->j), we need to reverse the linked list provided that the reversing will be done in a manner like -</p>
  
  <p>Reverse 1st element (a)</p>
  
  <p>Reverse next 2 elements (a->c->b)</p>
  
  <p>Reverse next 3 elements (a->c->b->f->e->d)</p>
  
  <p>Reverse next 4 elements (a->c->b->f->e->d->j->i->h->g)</p>
  
  <p>....</p>
  
  <p>....</p>
  
  <p>I am looking for a PHP function code which takes the linked list $link and reverse it in above manner with least time complexity. </p>
  
  <p>For help, I am adding my code below for linked list. The function reverseLinkedList has to be completed to perform this specific reverse operation -</p>
  
  <pre><code>class ListNode
  {
      public $data;
      public $next;
  
      function __construct($data)
      {
          $this-&gt;data = $data;
          $this-&gt;next = NULL;
      }
  
      function read_node()
      {
          return $this-&gt;data;
      }
  }
  
  class LinkList
  {
      private $first_node;
      private $last_node;
      private $count;
  
      function __construct()
      {
          $this-&gt;first_node = NULL;
          $this-&gt;last_node = NULL;
          $this-&gt;count = 0;
      }
  
      function size()
      {
          return $this-&gt;count;
      }
  
      public function read_list()
      {
          $listData = array();
          $current = $this-&gt;first_node;
          while($current != NULL)
          {
              echo $current-&gt;read_node().' ';
              $current = $current-&gt;next;
          }
      }
  
      public function reverse_list()
      {
          if(($this-&gt;first_node != NULL)&amp;&amp;($this-&gt;first_node-&gt;next != NULL))
          {
              $current = $this-&gt;first_node;
              $new = NULL;
  
              while ($current != NULL)
              {
                  $temp = $current-&gt;next;
                  $current-&gt;next = $new;
                  $new = $current;
                  $current = $temp;
              }
              $this-&gt;first_node = $new;
          }
      }
  
      public function read_node($position)
      {
          if($position &lt;= $this-&gt;count)
          {
              $current = $this-&gt;first_node;
              $pos = 1;
              while($pos != $position)
              {
                  if($current-&gt;next == NULL)
                      return null;
                  else
                      $current = $current-&gt;next;
  
                  $pos++;
              }
              return $current-&gt;data;
          }
          else
              return NULL;
      }
  
      public function insert($data)
      {
          $new_node = new ListNode($data);
  
          if($this-&gt;first_node != NULL)
          {
              $this-&gt;last_node-&gt;next = $new_node;
              $new_node-&gt;next = NULL;
              $this-&gt;last_node = &amp;$new_node;
              $this-&gt;count++;
          }
          else
          {
              $new_node-&gt;next = $this-&gt;first_node;
              $this-&gt;first_node = &amp;$new_node;
  
              if($this-&gt;last_node == NULL)
                  $this-&gt;last_node = &amp;$new_node;
  
              $this-&gt;count++;
          }
      }
  }
  
  
  //Create linked list
  $link1 = new LinkList();
  
  //Insert elements
  $link1-&gt;insert('a');
  $link1-&gt;insert('b');
  $link1-&gt;insert('c');
  $link1-&gt;insert('d');
  $link1-&gt;insert('e'); 
  $link1-&gt;insert('f'); 
  $link1-&gt;insert('g'); 
  $link1-&gt;insert('h');
  $link1-&gt;insert('i'); 
  $link1-&gt;insert('j'); 
  
  echo "&lt;b&gt;Input :&lt;/b&gt;&lt;br&gt;";       
  $link1-&gt;read_list();
  
  
  //function to reverse linked list in specified manner
  function reverseLinkedList(&amp;$link1)
  {
    //Logic to reverse the linked list $link1
  }
  ///function to reverse linked list in specified manner
  
  //Reverse current linked list $link1
  reverseLinkedList($link1);
  
  echo "&lt;br&gt;&lt;br&gt;&lt;b&gt;Output :&lt;/b&gt;&lt;br&gt;";       
  $link1-&gt;read_list();
  </code></pre>