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 ...
随机推荐
- Hadoop基于Protocol Buffer的RPC实现代码分析-Server端
http://yanbohappy.sinaapp.com/?p=110 最新版本的Hadoop代码中已经默认了Protocol buffer(以下简称PB,http://code.google.co ...
- WCF编程系列(三)地址与绑定
WCF编程系列(三)地址与绑定 地址 地址指定了接收消息的位置,WCF中地址以统一资源标识符(URI)的形式指定.URI由通讯协议和位置路径两部分组成,如示例一中的: http://loc ...
- 转:jQuery对象与dom对象的转换
jQuery对象与dom对象的转换 发布时间:September 20, 2007 分类:JavaScript <新站上线的手记> <Discuz!多附件上传选择框之jQuery版& ...
- Java多线程概述
/*多线程1.首先说进程,进程---就是正在进行的程序 每一个进程都有一个执行程序.该顺序是一个执行路径,或者叫一个控制单元 2.线程:就是进程中的一个独立的进程单元 线程在控制着 ...
- Config spec rules for elements in subbranches
Quote from: Config spec rules for elements in subbranches The following is an example of a config s ...
- [Guava官方文档翻译] 6. 用Guava辅助Throwable异常处理 (Throwables Explained)
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3537508.html ,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体 ...
- ubuntu下编译安装PHP
首先配置configure // ./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --wit ...
- win7 64位。未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
win7或win8 64位调试程序,出现这样的错误提示:未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序 解决方法如下: 方法一:“设置应用程序池默认属性”/“常规”/” ...
- php导出word(可包含图片)
为大家介绍一个 php 生成 导出word(可包含图片)的代码,有需要的朋友可以参考下. 之前介绍过php生成word的例子,只是不能包含图片与链接. 今天 为大家介绍一个 php 生成 导出word ...
- 《Junit实战》读书笔记
核心原则:任何没有经过自动测试的程序功能都可以当做不存在 单元测试框架的大三规则: 1.每个单元测试都必须独立于其他所有单元测试而运行 2.框架应该以单个测试为单元来检测和报告错误 3.应该易于定义要 ...