反转链表
来源:互联网 发布:java商品信息管理系统 编辑:程序博客网 时间:2024/06/07 05:35
输入一个链表,反转链表后,输出链表的所有元素。
通过递归的方式修改指针
/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode ReverseList(ListNode head) { if(head == null) return null ; ListNode temp = head ; ListNode ans = change(head) ; temp.next = null ; return ans ; } public ListNode change(ListNode head){ if(head.next == null) return head ; ListNode next = head.next ; ListNode node = change(next) ; next.next = head ; return node; }}
直接遍历修改指针
/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode ReverseList(ListNode head) { ListNode pre = null ; while(head != null){ ListNode next = head.next ; head.next = pre ; pre = head ; head = next ; } return pre ; }}
0 0
- 链表反转
- 反转链表
- 单向链表反转
- 链表反转:
- 链表的反转
- 链表反转
- 反转单向链表
- 链表反转
- 单向链表反转
- 单向链表反转
- 链表的反转
- 链表反转
- 反转链表
- 链表反转
- 链表反转
- 链表反转
- 链表反转
- 链表的反转
- glide加载图片
- “玲珑杯”ACM比赛 Round #7
- SSH框架之Hibernate的继承映射的形式二(三张表)
- Android实现推送方式解决方案
- Android系统权限和root权限
- 反转链表
- tiny-cnn配置
- 找老乡
- 设计模式——策略模式
- Linux CentOS6.x ip设置(网卡设置)
- FSL i.MX6 UltraLite EVK OP-TEE的移植
- 疯狂的程序员 --1
- 采用Apache+mod_wsgi在windows下搭建Flask项目(本地部署Flask的生产环境)
- IntelliJ Idea 常用快捷键列表