public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
} // 添加新的结点
public void add(int newval) {
ListNode newNode = new ListNode(newval);
if (this.next == null)
this.next = newNode;
else
this.next.add(newval);
} // 打印链表
public void print() {
System.out.print(this.val);
if (this.next != null) {
System.out.print("-->");
this.next.print();
} }
}
import java.util.Stack;

public class ListReverse {
public ListNode ReverseList(ListNode head) {
Stack<ListNode> stack = new Stack<ListNode>();
ListNode node = head;
if (head == null) {
return null;
}
while (node != null) {
stack.push(node);
node = node.next;
}
ListNode newHead = new ListNode(stack.pop().val);
ListNode tmp = newHead;
while (stack.size() > 0) {
node = stack.pop();
ListNode newtmp = new ListNode(node.val);
tmp.next = newtmp;
tmp = newtmp;
}
return newHead; // 在最后添加上null作为结束标志
} public static void main(String[] args) {
ListNode l1 = null; // 创建链表对象 l1 (对应有参 和 无参 构造方法)
if (l1 != null) {
l1.print();
ListReverse lr = new ListReverse();
ListNode reverseList = lr.ReverseList(l1);
System.out.println();
reverseList.print();
}else{ } }
}

最新文章

  1. ajax删除DB数据
  2. JS函数的上下文环境
  3. jQuery和JS原生方法对比
  4. 【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化
  5. PHP iconv()函数转字符编码的问题(转)
  6. 使用json格式的数据进行通信
  7. C++_归并排序(纯C版)
  8. Java下拼接运行动态SQL语句
  9. sgu 104 Little Shop of Flowers
  10. cx_Oracle ORA-24315: 非法的属性类型的解决办法
  11. POJ2236 Wireless Network 并查集
  12. 工厂方法模式 - OK
  13. [Q]图框识别问题
  14. 启动ipython notebook(jupyter)
  15. SQL server 数据库(视图、事物、分离附加、备份还原))
  16. 咦,好像可以自己做个webapi框架了-IRouteHandler的使用
  17. 有史以来功能最全,使用最简单的excel导入/导出工具
  18. Spark:相关错误总结
  19. Day7 子类调用父类的方法supper 绑定方法与非绑定方法
  20. C#类继承中构造函数的执行序列

热门文章

  1. 【论文考古】量化SGD QSGD: Communication-Efficient SGD via Gradient Quantization and Encoding
  2. Flutter ChartSpace:通过跨端 Canvas 实现图表库
  3. 浅谈 SOLID 原则
  4. CF1106F题解
  5. 1、如何抓取Modbus TCP/UDP 数据包实战
  6. Python之GUI用户界面Tkinter(一)
  7. 无法更新apt镜像源?树莓派安装最新版Debian11(bullseye)arm64位系统步骤
  8. K8S搭建自动化部署环境 Jenkins下载、安装和启动
  9. 安装配置Snort和barnyard2
  10. 10ISE14.7和modelsim10.5关联编译库