代码:

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 单链表逆序的更多相关文章

  1. 基于visual Studio2013解决面试题之0504单链表逆序

     题目

  2. 链表逆序(JAVA实现)

    题目:将一个有链表头的单向单链表逆序 分析: 链表为空或只有一个元素直接返回: 设置两个前后相邻的指针p,q,使得p指向的节点为q指向的节点的后继: 重复步骤2,直到q为空: 调整链表头和链表尾: 图 ...

  3. Java 实现单链表反序

    //单链表反序 public class SingleLinkedListReverse { public static void main(String[] args) { Node head = ...

  4. 链表逆序,java实现

    package com.cskaoyan.linkedlist; //反转数组 public class LinkedListDemo2 { public static Node reverse(No ...

  5. Java单链表反转 详细过程

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...

  6. Reverse Linked List II 单向链表逆序(部分逆序)

    0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...

  7. ZT 链表逆序

    链表逆序 原帖地址http://blog.csdn.net/niuer09/article/details/5961004 分类: C/C++2010-10-23 17:23 18425人阅读 评论( ...

  8. java 单链表 练习

    练习一下java单链表的简单习题 package com.test1; import java.util.Stack; public class SingleListDemo { /** * 返回单链 ...

  9. C# 单向链表 逆序(递归)

    static void Main(string[] args) { while (true) { LinkedList L = new LinkedList(); L.Add(new Node(&qu ...

随机推荐

  1. 如何入侵Linux操作系统

    我发现了一个网站,于是常规入侵.很好,它的FINGER开着,于是我编了一个SHELL,aaa帐号试到zzz(by the way,这是我发现的一个网上规律,那就是帐号的长度与口令的强度成正比, 如果一 ...

  2. JQuery弹出层,实现弹层切换,可显示可隐藏。

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  3. iOS开发——百度云推送

    由于公司项目是集成的极光推送,详见下一篇博客. 集成百度推送大体相当,最好都参考官方文档集成,官方文档或官方网站教程是最好的博客. 百度Push服务SDK用户手册(iOS版) http://push. ...

  4. Warning: Attempt to present * on * which is already presenting *

    Warning: Attempt to present (要被presented的控制器)  on (哪个控制器来presenting) which is already presenting (已经 ...

  5. 访问图像中的像素[OpenCV 笔记16]

    再更一发好久没更过的OpenCV,不过其实写到这个部分对计算机视觉算法有所了解的应该可以做到用什么查什么了,所以后面可能会更的慢一点吧,既然开了新坑,还是机器学习更有研究价值吧... 图像在内存中的存 ...

  6. iOS 详细解释@property和@synthesize关键字

    /** 注意:由@property声明的属性 在类方法中通过下划线是获取不到的 必须是通过 对象名.属性 才能获取到!- @property和@synthesize关键字是针对成员变量以及get/se ...

  7. redis_笔记

    1.redis是什么 这个问题的结果影响了我们怎么用redis.如果你认为redis是一个key value store,那可能会用它来 代替mysql:如果认为它是一个可以持久化的cache,可能只 ...

  8. WebClient以POST方式发送Web请求

    本例使用WebClient以POST方式发送Web请求并下载一个文件,难点是postData的构造,发送Web请求时有的网站要求可能要求 Cookies前后一致.其中application/x-www ...

  9. PHOTOSHOP 制作虚线和实线

    1.制作实线可以直接用直线工具,选择合适的粗细大小. 2. 制作虚线首先要用钢笔或者绘图工具画出所需要的形状,如弧线,圆形等等     然后在路径面板中用画笔描边,画笔需要提前设置好粗细和间距,用方形 ...

  10. Python3 网络编程

    虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Microsoft都有各自的 ...