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 ...
随机推荐
- (译文)12个简单(但强大)的JavaScript技巧(二)
原文链接: 12 Simple (Yet Powerful) JavaScript Tips 其他链接: (译文)12个简单(但强大)的JavaScript技巧(一) 强大的立即调用函数表达式 (什么 ...
- h2database源码浅析:锁与MVCC
Table Level Locking The database allows multiple concurrent connections to the same database. To mak ...
- rest的config
<个人积累,转载请注明出处> 新手写rest wcf经常会报配置文件异常.我为了避免这种问题,将自己配好的config放这里,用的时候将ABC改成自己的,粘贴就行了. ABC是什么我就不赘 ...
- ASP实现清除HTML标签,清除 空格等编码
'清除HTML格式 Function RemoveHTML(strText) Dim RegEx Set RegEx = New RegExp RegEx.Global = True '清除HTML标 ...
- partial与sorted
import functools sorted_ignore_case = functools.partial(sorted,cmp=lambda s1, s2: cmp(s1.upper(), s2 ...
- and or判别
and 和 or涉及到短路运算,python把0,'',none看做false,其余是true, 对于a and b,若a是true则,返回b,若a是false则返回a(因为a是true还需要判断b, ...
- C# 高精度减法 支持小数(待优化)
是现实思路 1,先小数点补位,8913758923475893274958738945793845-4893127498372459823745324532453245.284929384729837 ...
- Java实战之04JavaWeb-02Request和Response
一.Response和Request的生态环境 二.HttpServletResponse---代表响应对象 1.设置状态码 void setStatus(int sc) 状态码: 200:一切正常 ...
- mysql innodb 数据打捞(二)innodb 页面打捞编程
有了页面的结构和特征,需要编程实现数据库页面的打捞工作: 为了方便windows and linux 的通用,计划做成C语言的控制台应用,并且尽量只用ansi c;关于多线程,计划做成多线程的程序,最 ...
- 关于ligerUi的ligertree的初始化默认选中指定项目的方法
LigerUi中ligerTree官方示例代码片段: var parm = function (data) { return data.text.indexOf('节点1.3') == 0; }; t ...