Home  >  Article  >  Backend Development  >  PHP implementation of single linked list flip operation example explanation

PHP implementation of single linked list flip operation example explanation

jacklove
jackloveOriginal
2018-07-06 17:56:181661browse

This article mainly introduces the implementation of singly linked list flipping operation in PHP, and analyzes the definition, traversal, recursion, flipping and other related operation skills of PHP singly linked list in the form of examples. Friends in need can refer to this article

The example describes the implementation of singly linked list flipping operation in PHP. Share it with everyone for your reference, the details are as follows:

When a sequence only contains links pointing to its successor nodes, the linked list is called a singly linked list.

The definition of a singly linked list and the flip operation method are given here:

value = $value;
  }
  public function getValue(){
    return $this->value;
  }
  public function setValue($value){
    $this->value = $value;
  }
  public function getNext(){
    return $this->next;
  }
  public function setNext($next){
    $this->next = $next;
  }
}
//遍历,将当前节点的下一个节点缓存后更改当前节点指针 
function reverse($head){
  if($head == null){
    return $head;
  }
  $pre = $head;//注意:对象的赋值
  $cur = $head->getNext();
  $next = null;
  while($cur != null){
    $next = $cur->getNext();
    $cur->setNext($pre);
    $pre = $cur;
    $cur = $next;
  }
  //将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head 
  $head->setNext(null);
  $head = $pre;
  return $head;
}
//递归,在反转当前节点之前先反转后续节点 
function reverse2($head){
  if (null == $head || null == $head->getNext()) {
    return $head;
  }
  $reversedHead = reverse2($head->getNext());
  $head->getNext()->setNext($head);
  $head->setNext(null);
  return $reversedHead;
}
function test(){
  $head = new Node(0);
  $tmp = null;
  $cur = null;
  // 构造一个长度为10的链表,保存头节点对象head  
  for($i=1;$i<10;$i++){
    $tmp = new Node($i);
    if($i == 1){
      $head->setNext($tmp);
    }else{
      $cur->setNext($tmp);
    }
    $cur = $tmp;
  }
  //print_r($head);exit;
  $tmpHead = $head;
  while($tmpHead != null){
    echo $tmpHead->getValue().' ';
    $tmpHead = $tmpHead->getNext();
  }
  echo "\n";
  //$head = reverse($head);
  $head = reverse2($head);
  while($head != null){
    echo $head->getValue().' ';
    $head = $head->getNext();
  }
}
test();
?>

Running results:

0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0

Articles you may be interested in:

Explanation of how to merge two ordered arrays in PHP

Detailed explanation of the method of implementing the Joseph ring problem in PHP

Explanation of the method of using Passport to implement Auth authentication in Laravel5.5

The above is the detailed content of PHP implementation of single linked list flip operation example explanation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn