反转链表 - 牛客剑指offer(n)
2018-07-10 23:27
190 查看
题目描述
输入一个链表,反转链表后,输出新链表的表头。
思路:
这道题只需要解决:保持head持续往下的同时,记录head的前一个节点;
所以用一个pre存储前一个节点,next存储下一个节点,画个图就很容易理解;public class Solution { public ListNode ReverseList(ListNode head) { if(head==null) return null; ListNode pre=null; ListNode next=null; while(head!=null){ next=head.next;//保存下一个节点 head.next=pre;//逆转 pre=head;//保存前一个节点 head=next;//保持持续往下 } return pre; } }
阅读更多
相关文章推荐
- 牛客《剑指Offer》 反转链表
- 《剑指offer》面试题16—反转链表
- 反转链表(剑指offer)
- python 剑指offer系列:反转链表
- 【牛客网】反转链表
- 《剑指offer》学习之--反转链表与合并两个排序链表
- 《剑指offer》编程题java实现(十三):反转链表
- 剑指offer面试题24:反转链表-java
- 《剑指Offer》学习笔记--面试题16:反转链表
- 反转链表 --剑指offer
- 剑指Offer编程练习015——反转链表
- 剑指offer面试题17,18:反转链表+合并有序链表
- 《剑指offer》刷题笔记(代码的鲁棒性):反转链表
- 剑指offer系列之十四:反转链表
- 反转链表--《剑指offer》
- 牛客:剑指offer:两个链表的第一个公共结点(Java)
- 剑指Offer--016-反转链表
- 《剑指Offer》 反转链表
- 剑指offer----反转链表
- 剑指offer——面试题16:反转链表