* 参照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. stm32 connot enter debug mode

    dap 可以发现设备,stlink jlink 均无法发现设备,但是都不能下载.connot enter debug mode ,发现是vdda 未连接

  2. NOIP 模拟 6 大佬

    这道题是一道数学期望,考场上想的是,每相邻 \(k\) 天之间有 \(k-1\) 天是重合的,所以每两端之间肯定是有影响的. 结果啪啪打脸 这道题其实不用考虑每两段之间的影响,因为在上一段的每种排法, ...

  3. WPF---数据绑定(二)

    一.绑定到非UI元素 上篇中,我们绑定的数据源均是派生自UIElement的WPF元素.本篇描述的绑定数据源是一个我们自定义的普通的类型. 注:尽管绑定的数据源可以是任意类型的对象,但Path必须总是 ...

  4. WPF 获取主线程

    WPF线程获取UI线程   WPF中只能是UI线程才可以改变UI控件相关,当采用多线程工作时,可用以下代码获取 UI线程进行操作: App.Current.Dispatcher.Invoke((Act ...

  5. shiro(二)

    public class AuthorizerTest { @Test public void testIsPermitted() { login("classpath:shiro-auth ...

  6. JDBC基础篇(MYSQL)——使用CallabeStatement调用存储过程

    注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接: package day04_callable; import java.sql.CallableStatement; import ...

  7. Go与接口:接口即约定

    接口 接口类型是对其他类型行为的概括与抽象.我们可以通过接口来约定某一类通用行为.Go语言的接口是隐式的:只要实现接口A的所有方法就代表实现了接口A. 接口即约定 接口是什么样的? package i ...

  8. unity优化 — UGUI纹理格式的选择

    首次界面打开加载的资源(如 贴图)会被缓存在内存中,再次打开界面由于内存中已有了资源 所以会更快.如何让首次打开界面会更快呢? 图片是否进行了有效的压缩.Android 平台下不带透明通道 优先使用E ...

  9. TFRecord读写简介+Demo 基于Ubuntu18.04+Tensorflow1.12 无WARNING

    简介 TFRecord是TensorFlow官方推荐使用的数据格式化存储工具. 它规范了数据的读写方式. 只要生成一次TFRecord,之后的数据读取和加工处理的效率都会得到提高. 将图片转换成TFR ...

  10. Python3-sqlalchemy-orm 创建关联表带外键并查询数据

    #-*-coding:utf-8-*- #__author__ = "logan.xu" import sqlalchemy from sqlalchemy import crea ...