php 单向链表反转 reverse (没有空的头结点)
* 参照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 (没有空的头结点)的更多相关文章
- C++ 单向链表反转
单向链表反转,一道常见的面试题,动手实现下. #include "stdafx.h" #include <stdlib.h> struct Node{ int data ...
- Java实现单向链表反转
public class LinkedListTest { public static void main(String[] args) { Node A = new Node("A&quo ...
- Linux C 数据结构 ->单向链表<-(~千金散尽还复来~)
之前看到一篇单向链表的博文,代码也看着很舒服,于是乎记录下来,留给自己~,循序渐进,慢慢 延伸到真正的内核链表~(敢问路在何方?路在脚下~) 1. 简介 链表是Linux 内核中最简单,最普通的数据结 ...
- Linux C 数据结构 ->单向链表
之前看到一篇单向链表的博文,代码也看着很舒服,于是乎记录下来,留给自己~,循序渐进,慢慢 延伸到真正的内核链表~(敢问路在何方?路在脚下~) 1. 简介 链表是Linux 内核中最简单,最普通的数据结 ...
- 数据结构(1) 第一天 算法时间复杂度、线性表介绍、动态数组搭建(仿Vector)、单向链表搭建、企业链表思路
01 数据结构基本概念_大O表示法 无论n是多少都执行三个具体步骤 执行了12步 O(12)=>O(1) O(n) log 2 N = log c N / log c N (相当于两个对数进行了 ...
- Reverse Linked List(反转单向链表)
来源:https://leetcode.com/problems/reverse-linked-list Reverse a singly linked list. 递归方法:递归调用直到最后一个节点 ...
- [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 ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- Reverse Linked List II 单向链表逆序(部分逆序)
0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...
随机推荐
- Redis奇怪的姿势
Redis奇怪的姿势 写在前面 之前渗透 摸鱼 时和小伙伴发现了一个redis,存在未授权,是win服务器但是没有路径,度娘了之后发现了这个姿势,特此学习记录一下. 写入启动项 环境搭建 window ...
- Maven 下载、安装与配置
一.需要准备的东西 确定电脑上已经成功安装JDK 二.下载与安装 1. 前往https://maven.apache.org/download.cgi下载最新版的Maven程序: 注意:Maven3. ...
- 微信小程序自定义顶部
wxml <view style="height:{{titleHeight}}px;background:{{background}}" class="user- ...
- C#基础知识---is与as
一.is与as对比 is检查一个对象是否兼容于指定的类型,并返回一个Boolean值:true或者fasle. 注:is操作符永远不会抛出异常 经常按如下方法使用: ClassA { .... } O ...
- promise错误处理的三种方法
promise碰到then,也就是resolve或者reject的时候是异步的,所以try...catch对它是没有用的 1.then(resolve,reject); then方法中第二个回调,是 ...
- T-SQL - query01_创建数据库|创建表|添加数据|简单查询
时间:2017-09-29 整理:byzqy 本篇以"梁山好汉花名册"为例,记录MS SQLServer T-SQL语句的使用,包含命令: 创建数据库 | 删除数据库 创建表 | ...
- WIN7下安装Python3.7和labelImg-1.7.0
安装python3.7 官方https://www.python.org/downloads/windows/,下载windows 64bit python3.7版本 用Administrator权限 ...
- 接口自动化-python+requests+pytest+csv+yaml
本套代码和逻辑 是本人的劳动成果,如果有转载需要标注, 非常适合公司做项目的同学!!!小白也可以学哦! 1.项目目录 2.公共方法的封装 2.1如果不用配置文件 可以使用这个方法进行封装--但是有一 ...
- WAMP 2.5 无法访问局域网的解决方法
打开Apache配置文件 httpd.conf (该文件在wamp\bin\apache\apache2.4.9\conf) DocumentRoot "d:/wamp/www/" ...
- MyBatis学习总结(三)——MyBatis配置文件配置的优化
一.连接数据库的配置单独放在一个properties文件中 上文 连接数据库的配置写在 mybatisConf.xml中,本文直接放在 db.properties 中, 在mybatisConf.xm ...