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-& ...
随机推荐
- SpringBoot 整合 SpringSecurity 梳理
文档 Spring Security Reference SpringBoot+SpringSecurity+jwt整合及初体验 JSON Web Token 入门教程 - 阮一峰 JWT 官网 Sp ...
- ASP.NET Core 修改开源协议为MIT,.NET全平台 MIT协议开源了
2021年7月23日,.NET开发团队完成了所有的.NET平台的相关框架的MIT协议更改,我们可以通过 https://github.com/dotnet/aspnetcore/issues/1887 ...
- NOIP 模拟 $22\; \rm d$
题解 很好的贪心题 考虑去掉的矩形一定是几个 \(a\) 最小的,几个 \(b\) 最小的,枚举去掉几个 \(a\),剩下的去掉 \(b\) 先对 \(a\) 排序,用小根堆维护 \(b\) ,记录哪 ...
- 题解 Strange Housing
传送门 首先想了黑白染色,发现不会染 其实可以考虑如何动态地维护出这个点集 发现题面里对不在点集之中的点之间的连边没有要求 所以考虑不断向图中加点,为了满足要求,每次取一个与当前新图中相连的点 若它与 ...
- @Transactional-同一个类中方法自调,调用方法事物失效
问题分析 一个类中的方法调用另一个事物传播性为创建事物的方法,调用的方法事物失效? SpringAOP 代理的Service对象调用了其方法,这个方法再去调用这个Service中的其他方法是没有使用A ...
- flutter查看安全码SHA1
最近flutter技术调研高德地图插件时,要用到安全码,可以打开cmd,键入一下命令查看.(注意路径用户名yourusernamehere改为自己的) keytool -list -v -keysto ...
- dataTemplate 使用
App ----------------------------------------------------------------- <Application x:Class=" ...
- docker ubuntu中文乱码
docker ubuntu18.04 使用cat查看中文正常,使用vim查看中文乱码. 解决此问题需要修改"/etc/profile"文件. 1.修改前查看本地使用的语言环境: l ...
- mfc HackerTools释放资源
作用: 在VC环境中除了我们所常用的Dialog.Menu和Bitmap等标准资源类型之外,它还支持自定义资源类型(Custom Resource),我们自定义的资源类型能做些什么呢?呵呵,用处多多. ...
- Float浮动(慕课网学习笔记)
float浮动 属性:值 意义 float:left 左浮动 float:right 右浮动 float:none 不浮动 float:inherit 继承父元素浮动属性,若父元素没有浮动属性则失效 ...