链表——反转链表
来源:互联网 发布:女毒耳机知乎 编辑:程序博客网 时间:2024/06/06 01:14
了解反转链表的通法:
方法一:循环
遍历一遍链表,利用一个辅助指针ne,存储遍历过程中当前指针cur指向的下一个元素,然后将当前节点元素的指针反转后,利用已经存储的指针往后面继续遍历。源代码如下:
public ListNode reverseList(ListNode head) { if(head==null) return head; ListNode pre=head; ListNode cur=head.next; ListNode ne=null; while(cur!=null) { ne=cur.next; cur.next=pre; pre=cur; cur=ne; } head.next=null; head=pre; return head; }
方法二:递归
public class Solution { public ListNode ReverseList(ListNode head) { if(head == null||head.next == null) return head; ListNode phead=ReverseList(head.next); head.next.next=head; head.next=null; return phead; }}
0 0
- 链表——反转链表
- 链表——成对的反转链表
- Microsoft100——003.链表反转
- 程序2——链表反转
- 剑指offer——反转链表
- 《剑指offer》——反转链表
- 《剑指offer》——反转链表
- 反转链表——递归实现
- 剑指offer——反转链表
- 剑指offer——反转链表
- 剑指offer——反转链表
- 剑指offer——反转链表
- 剑指offer——反转链表
- 剑指offer——反转链表
- 剑指Offer——反转链表
- 剑指offer——反转链表
- 剑指offer—反转链表
- 面试题16—反转链表
- Node.js 创建第一个应用(2)
- Android SystemUI 初探
- 并发导论
- CTSC2016&&APIO2016爆零记
- C语言实现单词翻转
- 链表——反转链表
- [数据结构]二叉树的遍历及构造树
- 大端小端
- [kuangbin带你飞]专题十二 基础DP1 G - 免费馅饼
- cPanel下如何设置MIME
- PRML 读书笔记-Chapter1
- 多态的例子
- 在OpenWrt中利用nixio完成socket通信
- java中的静态变量和实例变量的生命周期