剑指Offer面试题16 & Leetcode206
来源:互联网 发布:mui案例源码下载 编辑:程序博客网 时间:2024/06/15 13:21
剑指Offer面试题16 & Leetcode206
Reverse Linked List 反转链表
Reverse a singly linked list.
解题思路
考虑:从链表的头结点开始依次反转链表,即某节点的next指针指向它的前一个节点。这里为了防止链表断裂,需要提前将某节点的next指针原先指向的节点保存下来。所以使用了三个ListNode节点来分别保存pre、p、nxt节点。每次循环所完成的工作为:保存节点p的next指针指向的节点并赋给nxt节点,将p的next指针指向pre;为下次循环做准备,pre指向p,p指向nxt。
Solution
public ListNode reverseList(ListNode head) { if(head==null || head.next==null) return head; ListNode pre = head; ListNode p = head.next; pre.next = null; ListNode nxt; while(p!=null){ nxt = p.next; p.next = pre; pre = p; p = nxt; } return pre; }
0 0
- 剑指Offer面试题16 & Leetcode206
- 剑指offer面试题16
- 【面试题】剑指offer 16
- 剑指offer 面试题
- 剑指offer面试题
- 剑指offer 面试题16 反转单链表
- 《剑指offer》面试题16扩展题
- 剑指offer面试题总结
- 剑指offer面试题06
- 剑指offer面试题 04
- 剑指offer 面试题07
- 剑指offer面试题08
- 剑指offer面试题42
- 剑指offer面试题1
- 剑指offer面试题3
- 剑指offer面试题4
- 剑指offer面试题5
- 剑指offer面试题6
- Problem D: 线性表的基本操作
- Spring AOP实现方式
- 新手写LinuxI2C驱动程序
- Android中图片加载框架Picasso的使用
- Android 官网Train阅读记录——4
- 剑指Offer面试题16 & Leetcode206
- Java笔试题解(4)
- Spring中AOP切面的定义
- platform总线驱动
- 利用mybatis-generator自动生成代码
- elasticsearch 创建索引
- Leetcode学习(27)—— Add Strings
- http协议301、302的原理和实现
- redis 清空缓存命令