Java 单链表逆序
代码:
package com.wangzhu.linkedlist;
public class LinkedListDemo {
/**
* @param args
*/
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
Node head = null;
Node reverseHead = null;
// 链表长度为0
head = new Node();
for (int i = 0; i < 0; i++) {
linkedList.add(head, new Node(i));
}
System.out.print("未操作:");
linkedList.print(head);
reverseHead = linkedList.reverse(head);
System.out.print("反转后:");
linkedList.print(reverseHead);
System.out.println();
// 链表长度为1
head = new Node();
for (int i = 0; i < 1; i++) {
linkedList.add(head, new Node(i));
}
System.out.print("未操作:");
linkedList.print(head);
reverseHead = linkedList.reverse(head);
System.out.print("反转后:");
linkedList.print(reverseHead);
System.out.println();
// 链表长度为2
head = new Node();
for (int i = 0; i < 2; i++) {
linkedList.add(head, new Node(i));
}
System.out.print("未操作:");
linkedList.print(head);
reverseHead = linkedList.reverse(head);
System.out.print("反转后:");
linkedList.print(reverseHead);
System.out.println();
// 链表长度为3
head = new Node();
for (int i = 0; i < 3; i++) {
linkedList.add(head, new Node(i));
}
System.out.print("未操作:");
linkedList.print(head);
reverseHead = linkedList.reverse(head);
System.out.print("反转后:");
linkedList.print(reverseHead);
System.out.println();
// 链表长度为4
head = new Node();
for (int i = 0; i < 4; i++) {
linkedList.add(head, new Node(i));
}
System.out.print("未操作:");
linkedList.print(head);
reverseHead = linkedList.reverse(head);
System.out.print("反转后:");
linkedList.print(reverseHead);
System.out.println();
// 链表长度为5
head = new Node();
for (int i = 0; i < 5; i++) {
linkedList.add(head, new Node(i));
}
System.out.print("未操作:");
linkedList.print(head);
reverseHead = linkedList.reverse(head);
System.out.print("反转后:");
linkedList.print(reverseHead);
System.out.println();
// 未操作:反转后:
// 未操作:0
// 反转后:0
//
// 未操作:0 1
// 反转后:1 0
//
// 未操作:0 1 2
// 反转后:2 1 0
//
// 未操作:0 1 2 3
// 反转后:3 2 1 0
//
// 未操作:0 1 2 3 4
// 反转后:4 3 2 1 0
}
}
class Node {
int value;
Node next;
public Node() {
this.value = -1;
this.next = null;
}
public Node(int value) {
this.value = value;
this.next = null;
}
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
class LinkedList {
/**
* 后插法
*
* @param head
* @param node
*/
public void add(Node head, Node node) {
Node tempNode = head;
if (tempNode == null) {
return;
}
while (tempNode.next != null) {
tempNode = tempNode.next;
}
tempNode.next = node;
}
public void print(Node head) {
Node node = head;
if (node == null || node.next == null) {
return;
}
node = node.next;
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
}
/**
* 链表反转
*
* @param head
* @return
*/
public Node reverse(Node head) {
// 当链表为空,或链表没有元素
if (head == null || head.next == null) {
return head;
}
// 反转头节点
Node reverseHead = new Node();
Node firstNode = head.next;// 链表中节点的前一个节点
Node node = firstNode.next; // 链表中的节点
Node nextNode = null;
if (node != null) {
nextNode = node.next;// 链表中的节点的后一个节点
}
firstNode.next = null;
while (nextNode != null) {
node.next = firstNode;// 将当前节点的后一个一个改为其前一个节点(相对反转之前)
firstNode = node; // 将当前节点置为下一节点的前节点
node = nextNode;// 获取下一个节点
nextNode = nextNode.next;// 获取下一个节点的后一个节点
}
if (node != null) {
// 当节点个数大于1时
node.next = firstNode;
reverseHead.next = node;
} else {
// 当只有一个节点时
reverseHead.next = firstNode;
}
return reverseHead;
}
}
Java 单链表逆序的更多相关文章
- 基于visual Studio2013解决面试题之0504单链表逆序
题目
- 链表逆序(JAVA实现)
题目:将一个有链表头的单向单链表逆序 分析: 链表为空或只有一个元素直接返回: 设置两个前后相邻的指针p,q,使得p指向的节点为q指向的节点的后继: 重复步骤2,直到q为空: 调整链表头和链表尾: 图 ...
- Java 实现单链表反序
//单链表反序 public class SingleLinkedListReverse { public static void main(String[] args) { Node head = ...
- 链表逆序,java实现
package com.cskaoyan.linkedlist; //反转数组 public class LinkedListDemo2 { public static Node reverse(No ...
- Java单链表反转 详细过程
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...
- Reverse Linked List II 单向链表逆序(部分逆序)
0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...
- ZT 链表逆序
链表逆序 原帖地址http://blog.csdn.net/niuer09/article/details/5961004 分类: C/C++2010-10-23 17:23 18425人阅读 评论( ...
- java 单链表 练习
练习一下java单链表的简单习题 package com.test1; import java.util.Stack; public class SingleListDemo { /** * 返回单链 ...
- C# 单向链表 逆序(递归)
static void Main(string[] args) { while (true) { LinkedList L = new LinkedList(); L.Add(new Node(&qu ...
随机推荐
- 浏览器是如何运行HTML的?
什么是网页 网页(HTML page)是在浏览器(Browser)上运行并且可以与用户产生互动的应用程序. 此图为浏览器运行HTML 这个想说 ...
- ASP.NET MVC总结
一.概述 1.单元测试的NUnit, MBUnit, MSTest, XUnit以及其他的框架 2.ASP.NET MVC 应用的默认目录结构有三个顶层目录: Controllers.Models.V ...
- C#学习笔记13:静态方法、方法重载和ref、out参数
静态方法 调用:如果你写的方法和Main()方法在同一个类中,直接写方法名. 如果不在一个类中,需要类名.方法名(); 非静态方法: 调用:创建一个类的对象 对象名.方法名(); Person pe ...
- 用curl做异步操作
class CurlPost { /** * @desc curl 请求提交数组 * */ public function execute($method, $url, $fields = '', $ ...
- 解决某些手机RadioGroup中的RadioButton不居中的问题
问题:RadioButton中使用android:gravity="center"使其图片文字居中,在我的华为荣耀7手机上居中显示了,但在HUAWEI G606-T00却显示在右侧 ...
- AWK 脚本编写习惯
教训总结: 不能忽略了脚本语言的编写规范! 创建数组的时候初始化,特别是在for循环中使用的数组: u_count[; g_count[; 认真对待对象,特别是数组的命名: username_to_d ...
- 从零开始部署小型企业级虚拟桌面 -- Vmware Horizon View 6 For Linux VDI -- 结构规划
环境说明 注,本套环境所用机器全部是64位的. 管理服务器载体:安装win7操作系统,通过VMware Workstation安装4台虚拟机,用作vCenter,Connection Server,D ...
- GCD 多线程
Grand Central Dispatch (GCD)是Apple开发的一个多核编程的较新的解决方法.它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统.它是一个在线程池模式的基础上执行的 ...
- 性能更好的js动画实现方式——requestAnimationFrame
本文转载,原文地址:http://www.cnblogs.com/2050/p/3871517.html 用js来实现动画,我们一般是借助setTimeout或setInterval这两个函数,css ...
- IO流05_OutputStream和Writer输出流
[输出流中的字节流和字符流] [OutPutStream和Writer] [ OutputStream和Writer中包含的方法 ] void write(int c) 将指定的字节/字符 ...