206.反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]

方法一:

思路:比如,原本是cur.next = curr,变成curr.next=cur

 考虑遍历链表,并在访问各节点时修改 next 引用指向,算法流程见注释。

 过程:

 1-2-3-4-5-null

 变为

 null-1-2-3-4-5

 为什么需要一个临时结点temp:因为当cur.next=pre后,cur原本的指向后继结点的线就断了,为了能够继续向后执行,需要一个temp进行存储cur.next

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
 

 /**
 思路:比如,原本是cur.next = curr,变成curr.next=cur

 考虑遍历链表,并在访问各节点时修改 next 引用指向,算法流程见注释。
 过程:
 1-2-3-4-5-null
 变为
 null-1-2-3-4-5
 为什么需要一个临时结点temp:因为当cur.next=pre后,cur原本的指向后继结点的线就断了,为了能够继续向后执行,需要一个temp进行存储cur.next
  */
class Solution {
    public ListNode reverseList(ListNode head) {
        //判断head
        if(head == null){
            return head;
        }

        ListNode pre  = null;
        ListNode cur = head;

        //临时结点
        ListNode temp;
        
        //终止条件cur==null
        while(cur != null){
            temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;

        }

        return pre;



        



    }
}

方法二:递归

考虑使用递归法遍历链表,当越过尾节点后终止递归,在回溯时修改各节点的 next 引用指向。

recur(cur, pre) 递归函数:
终止条件:当 cur 为空,则返回尾节点 pre (即反转链表的头节点);
递归后继节点,记录返回值(即反转链表的头节点)为 res ;
修改当前节点 cur 引用指向前驱节点 pre ;
返回反转链表的头节点 res ;
reverseList(head) 函数:
调用并返回 recur(head, null) 。传入 null 是因为反转链表后, head 节点指向 null ;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */

/**
 * 思路:比如,原本是cur.next = curr,变成curr.next=cur
 * 
 * 考虑遍历链表,并在访问各节点时修改 next 引用指向,算法流程见注释。
 * 过程:
 * 1-2-3-4-5-null
 * 变为
 * null-1-2-3-4-5
 * 为什么需要一个临时结点temp:因为当cur.next=pre后,cur原本的指向后继结点的线就断了,为了能够继续向后执行,需要一个temp进行存储cur.next
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        // //判断head
        // if(head == null){
        // return head;
        // }

        // ListNode pre = null;
        // ListNode cur = head;

        // //临时结点
        // ListNode temp;

        // //终止条件cur==null
        // while(cur != null){
        // temp = cur.next;
        // cur.next = pre;
        // pre = cur;
        // cur = temp;

        // }

        // return pre;

        // 递归解法

        // 初始化
        // cur = head;
        // pre = null;
        return reverse(head, null);

    }

    private ListNode reverse(ListNode cur, ListNode pre) {
        if (cur == null)
            return pre;

        ListNode temp = cur.next;
        cur.next = pre;
        // pre = cur;
        // cur = temp;
        return reverse(temp, cur);
    }
}

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值