* 参照php标准库设计接口

http://php.net/manual/en/class.spldoublylinkedlist.php

* 反转单向链表

reverse方法, 其他的方法为了方便测试

<?php
/**
* Created by PhpStorm.
* User: Mch
* Date: 8/11/18
* Time: 00:25
*/
class Node {
public $value;
public $next;
public function __construct($data) {
$this->value = $data;
$this->next = null;
}
} class LinkedList implements Iterator, Countable {
private $head;
private $cur; public function __construct() {
$this->head = $this->cur = null;
} public function isEmpty() {
return is_null($this->head);
} public function count() {
$p = $this->head;
$count = 0;
while ($p !== null) {
$p = $p->next;
$count++;
}
return $count;
}
public function current() {
if ($this->isEmpty()) {
return null;
}
return $this->cur->value;
}
public function next() {
if (is_null($this->cur)) {
return null;
}
$this->cur = $this->cur->next;
}
public function rewind() {
$this->cur = $this->head;
} public function valid() {
return !is_null($this->cur);
}
public function key() {
$p = $this->head;
$key = 0;
while ($p !== $this->cur) {
$p = $p->next;
$key++;
}
return $key;
}
public function push($value) {
if ($this->isEmpty()) {
$this->head = new Node($value);
$this->cur = $this->head;
} else {
$this->cur = $this->head;
while ($this->cur->next !== null) {
$this->cur = $this->cur->next;
}
$this->cur->next = new Node($value);
}
return $this;
} public function forEach(callable $callback, mixed $userdata = null) {
$this->rewind();
while($this->valid()) {
call_user_func($callback, $this->current(), $this->key(), $userdata);
$this->next();
}
} public function reverse() {
if ($this->isEmpty())
return;
if ($this->count() < 2)
return;
$prev = null;
$cur = $this->head;
while ($cur) {
// save next
$next = $cur->next;
// move cur to head
$this->head = $cur;
$cur->next = $prev;
// iterate
$prev = $cur;
$cur = $next;
}
} }

  

* test

$list = new LinkedList();

for ($i = 65; $i < 91; $i++) {
$list->push(chr($i));
} $list->forEach(function($value, $index) {
printf("[%d] => %s<br />", $index, $value);
});
echo '-------------------<br />';
$list->reverse();
$list->forEach(function($value, $index) {
printf("[%d] => %s<br />", $index, $value);
});

  

* output:

[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
[5] => F

....

-------------------
[0] => Z
[1] => Y
[2] => X
[3] => W
[4] => V
[5] => U
...

php 单向链表反转 reverse (没有空的头结点)的更多相关文章

  1. C++ 单向链表反转

    单向链表反转,一道常见的面试题,动手实现下. #include "stdafx.h" #include <stdlib.h> struct Node{ int data ...

  2. Java实现单向链表反转

    public class LinkedListTest { public static void main(String[] args) { Node A = new Node("A&quo ...

  3. Linux C 数据结构 ->单向链表<-(~千金散尽还复来~)

    之前看到一篇单向链表的博文,代码也看着很舒服,于是乎记录下来,留给自己~,循序渐进,慢慢 延伸到真正的内核链表~(敢问路在何方?路在脚下~) 1. 简介 链表是Linux 内核中最简单,最普通的数据结 ...

  4. Linux C 数据结构 ->单向链表

    之前看到一篇单向链表的博文,代码也看着很舒服,于是乎记录下来,留给自己~,循序渐进,慢慢 延伸到真正的内核链表~(敢问路在何方?路在脚下~) 1. 简介 链表是Linux 内核中最简单,最普通的数据结 ...

  5. 数据结构(1) 第一天 算法时间复杂度、线性表介绍、动态数组搭建(仿Vector)、单向链表搭建、企业链表思路

    01 数据结构基本概念_大O表示法 无论n是多少都执行三个具体步骤 执行了12步 O(12)=>O(1) O(n) log 2 N = log c N / log c N (相当于两个对数进行了 ...

  6. Reverse Linked List(反转单向链表)

    来源:https://leetcode.com/problems/reverse-linked-list Reverse a singly linked list. 递归方法:递归调用直到最后一个节点 ...

  7. [Swift]LeetCode92. 反转链表 II | Reverse Linked List II

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  8. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  9. Reverse Linked List II 单向链表逆序(部分逆序)

    0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...

随机推荐

  1. HDFS 09 - HDFS NameNode 的高可用机制

    目录 1 - 为什么要高可用 2 - NameNode 的高可用发展史 3 - HDFS 的高可用架构 3.1 Standby 和 Active 的命名空间保持一致 3.2 同一时刻只有一个 Acti ...

  2. SSM自学笔记(四)

    8.面向切面编程AOP 1.Spring 的 AOP 简介 1.1 什么是 AOP AOP 为 Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运 ...

  3. 并发容器之ConcurrentHashMap

    JDK5中添加了新的concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能.因为同步容器将所有对容器状态的访问都 串行化了,这样保证了线程的安全性,所以这种方法的代价就是严重降低 ...

  4. Spark Core核心----RDD常用算子编程

    1.RDD常用操作2.Transformations算子3.Actions算子4.SparkRDD案例实战 1.Transformations算子(lazy) 含义:create a new data ...

  5. BeanUtils使用:从一个map集合中,拷贝到javaBean中(四)

    package beanutil; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; impo ...

  6. 哈希表(HashMap)分析及实现(JAVA)

    转自:http://www.java3z.com/cwbwebhome/article/article8/83560.html?id=4649 探讨Hash表中的一些原理/概念,及根据这些原理/概念, ...

  7. CentOS_Server with GUI入门

    安装模式: Server with GUI:基本的桌面系统,包括常用的桌面软件,如文档查看工具 Minimal:基本的系统,不含有任何可选的软件包 Basic Server :安装的基本系统的平台支持 ...

  8. ES6 class——name属性与new.target属性

    name属性与new.target属性 name属性: 1.类.name,输出的是类的名字. 2.如果是在类表达式中,类有名字,那么输出结果是类的名字:类没有名字的话,那么输出结果会是表达式中变量或者 ...

  9. ES6——静态属性与静态方法

    静态方法只能写在class内,constructor外.通过static关键字声明 静态属性只能写在class外,通过 类名.属性名 = 属性值 声明 //静态属性与静态方法(ES6明确规定,Clas ...

  10. Qt编译工程提示qt creator no rule to make target opencv2/core/hal/interface.h need by debug解决方法

    总是提示 qt creator no rule to make target opencv2/core/hal/interface.h need by debug解决方法: 也算是花了整整两个小时踩坑 ...