数据结构List示例(一):链表反转
来源:互联网 发布:网络教学的哪些专业好 编辑:程序博客网 时间:2024/06/06 09:18
链式存储结构就是两个相邻的元素在内存中可能不是相邻的,每一个元素都有一个指针域,指针域一般是存储着到下一个元素的指针。这种存储方式的优点是插入和删除的时间复杂度为O(1),不会浪费太多内存,添加元素的时候才会申请内存,删除元素会释放内存,。缺点是访问的时间复杂度最坏为O(n),关于查找的算法很少,一般只能遍历,这样时间复杂度也是线性(O(n))的了,频繁的申请和释放内存也会消耗时间。
顺序表的特性是随机读取,也就是访问一个元素的时间复杂度是O(1),链式表的特性是插入和删除的时间复杂度为O(1)。要根据实际情况去选取适合自己的存储结构。
链表就是链式存储的线性表。根据指针域的不同,链表分为单向链表、双向链表、循环链表等等。
链表反转也是比较常见的问题,一般解决方式是:分别设置三个指针。第一个指针保持结果,第二个指针指向当前的节点,第三个指针保存下一个节点。实现代码如下:
/**
* @Title: ListReverse.java
* @Package List
* @Description: 链表翻转问题
* @author peidong
* @date 2017-4-20 上午8:51:58
* @version V1.0
*/
/**
* 本例提供两种思路
* 1.设置三个变量,分别保存当前结点,下一个结点以及备用,依次反转
* 2.设置三个变量,分别保存头结点和尾结点以及备用,头尾对称反转
* 本例将利用迭代和递归两种方式实现第一种思路
*/
package List;
/**
* @ClassName: ListReverse
* @Description: TODO
* @date 2017-4-20 上午8:51:58
*
*/
public class ListReverse {
/**
*
* @ClassName: Node
* @Description: 构建结点
* @date 2017-4-20 上午9:00:32
*
*/
public static class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
/**
*
* @ClassName: LinkList
* @Description: 构建链表
* @date 2017-4-20 上午9:05:50
*
*/
public static class LinkList {
// 根结点
private Node root;
/**
*
* @Title: push
* @Description: 向链表插入数据
* @param @param data
* @return void
* @throws
*/
public void push(int data) {
Node cur = root;
if (root == null) {
// 分配内存空间
root = new Node(data);
return;
}
while (cur.next != null) {
cur = cur.next;
}
// 分配内存空间
cur.next = new Node(data);
}
/**
*
* @Title: linkReversr
* @Description: 非递归实现链表反转,例如4-3-2-1反转后1-2-3-4
* @param @return
* @return Node
* @throws
*/
public Node linkListReversre() {
// 当前结点
Node cur = null;
// 下一个结点
Node next = null;
// 前一个结点
Node pre = null;
// 判断边界条件,空链表
if (root == null) {
return null;
}
// 赋值
cur = root;
// 当链表非空时,执行反转
while (cur != null) {
// 保存后一个结点
next = cur.next;
// 注意此处反转的顺序不能乱
cur.next = pre;
pre = cur;
cur = next;
}
// 反转结束后修改头结点
root = pre;
// 返回
return root;
}
/**
*
* @Title: listRecReverse
* @Description: 使用递归的方式反转链表
* @param @return
* @return Node
* @throws
*/
public Node linkListRecReverse(Node cur){
// Node cur = root;
if(cur == null || cur.next == null){
return cur;
}
//递归
Node reverseRest = linkListRecReverse(cur.next);
//反转
cur.next.next = cur;
//源节点为空
cur.next = null;
return reverseRest;
}
public void printList() {
Node cur = root;
// 空表
if (root == null) {
return;
}
// 非空
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
}
}
/**
* @Title: main
* @Description: TODO
* @param @param args
* @return void
* @throws
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkList list = new LinkList();
//添加数据
list.push(10);
list.push(2);
list.push(3);
list.push(4);
list.push(5);
list.push(6);
//输出原始链表
list.printList();
System.out.println();
//非递归反转
list.linkListReversre();
//输出原始链表
list.printList();
}
}
- 数据结构List示例(一):链表反转
- 反转链表[数据结构]
- 反转链表[数据结构]
- 链表反转-数据结构
- Reverse Linked List(反转链表)
- 数据结构(二)之链表反转
- 数据结构(二)之链表反转
- 数据结构 -链表反转问题
- 数据结构之链表反转
- 数据结构之反转链表
- 数据结构 链表的反转
- 数据结构-反转链表详解
- 数据结构之链表反转
- 反转单向链表(reverse a singly linked list)(单个反转) [# 7]
- LeetCode-Reverse Linked List II(反转链表)
- 反转单向链表(reverse a singly linked list)
- [LeetCode-206] Reverse Linked List(链表反转)
- 链表-Reverse Linked List(反转单向链表)
- 单链表的快速排序
- Ehcache(02)——ehcache.xml配置简介
- JAVA开发之 4-编写我的第一个程序(helloworld)
- SVN客户端清空保存的密码重新输入的操作
- vs2013编译bitcoin源码(bitcoin-0.9.4)
- 数据结构List示例(一):链表反转
- LeetCode刷题(C++)——4Sum(Medium)
- dubbo接口配置说明
- 2017 ACM山东省赛 D
- Maven中junit打包问题
- Spring MVC 整合 Swagger2
- Js (Javascript) 回调的简单理解
- windows winhttp的编程例子,但是头文件和库文件,我的机子环境上没有,只有下载了头文件和库文件后才可以使用。
- 移动端/PC端网页开发建议