java实现单链表反转(遍历方式)
2015-11-23 15:36
519 查看
千辛万苦啊!!
public class NodeDemo { public static void main(String[] args) { Node head = new Node(0); Node temp = null; Node cur = null; for(int i=1;i<10;i++) { temp = new Node(i); if(i==1) head.setNextNode(temp); else cur.setNextNode(temp); cur = temp; } Node h = head; while(h!=null) { System.out.print(h.getData()+","); h = h.getNextNode(); } head = reverse(head); System.out.println(""); while(head!=null) { System.out.print(head.getData()+","); head = head.getNextNode(); } } public static Node reverse(Node node) { Node pre = node; Node cur = node.getNextNode(); Node next; while(cur!=null) { next = cur.getNextNode(); cur.setNextNode(pre); pre = cur; cur = next; } node.setNextNode(null); return pre; } } class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } public void setNextNode(Node node) { this.next = node; } public Node getNextNode() { return next; } public int getData() { return data; } }
相关文章推荐
- Spring中Bean的五个作用域
- Struts1 标签库
- Spring MVC interceptor 使用
- java 拦截器、过滤器、监听器
- resin 4.x 在MyEclipse配置
- Jsp+Servlet+JavaBean经典MVC模式理解
- Java 回调的概念
- Java反射机制
- struts2.0中struts.xml配置文件详解
- Java 理论与实践: 正确使用 Volatile 变量
- rsync 核心算法的Java实现
- 通过Eclipse创建SQLite数据库
- 基于spring的方式进行读写分离思考
- Java读带有BOM的UTF-8文件乱码原因及解决方法
- 详解Java编程中对线程的中断处理
- java算法
- Java 内存结构备忘录
- javascipt学习第2节---函数
- spring定时任务
- 举例解析Java多线程编程中需要注意的一些关键点