算法笔记--翻转链表

一、翻转整个链表

在这里插入图片描述

方法一:迭代

  1. 首先处理空链表 定义两个指针,一个指向当前节点,另一个指向上一个节点(初始为空)
  2. 遍历整个链表,每到一个节点,断开链表,并用临时变量记录后一个节点,当前的next指向前一个节点,前⼀个节点更新为当前节点,当前更新为刚刚记录的临时节点
  3. 返回头节点

在这里插入图片描述

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head == null) //处理空链表
            return null;
        ListNode cur = head;
        ListNode pre = null;
        while(cur != null){
            ListNode temp = cur.next; //断开链表,要记录后续⼀个
            cur.next = pre; //当前的next指向前⼀个
            pre = cur; //前⼀个更新为当前
            cur = temp; //当前更新为刚刚记录的后⼀个
         }
        return pre;
     }
}

方法二:递归

  1. 首先处理空链表
  2. 将head.next 放入递归方法中,进行翻转(不要跳进递归方法中),将head.next的尾部指向head节点,head.next置为空结点
  3. 返回新头结点
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述
public class Solution {
    public ListNode reverse(ListNode head) {
        if(head == null || head.next == null)
           return head;
        ListNode last = reverse(head.next); //反转下⼀个
        head.next.next = head; //逆转
        head.next = null; //尾部设置空结点
        return last;
     }
}

二、反转链表前 N 个节点

  1. 实现 reverse(ListNode head, int n),定义一个后驱临时节点
  2. 让翻转之后的链表与head节点连起来
  3. 让反转之后的 head 节点和后面的节点连起来
  4. 返回新的头节点
public class Solution {
    ListNode temp = null; // 后驱节点
    public ListNode reverseN(ListNode head, int n) {
        if(n == 1){
            // 记录第 n + 1 个节点
            temp = head.next;
            return head;        
        }
        //以 head.next 为起点,需要反转前 n - 1 个节点
        ListNode last = reverse(head.next,n-1); 
        head.next.next = head; //逆转
        head.next = temp; //让反转之后的 head 节点和后面的节点连起来
        return last;
     }
}

三、反转链表的一部分

public class Solution {
    ListNode temp = null; // 后驱节点
    public ListNode reverseMN(ListNode head,int m, int n) {
        if(m == 1){
            // 相当于反转前 n 个元素
             return(head , n);       
        }
        head.next = reverseMN(head.next, m-1, n-1);
        return head;
    }
    public ListNode reverseN(ListNode head, int n) {
        if(n == 1){
            temp = head.next;
            return head;        
        }
        //以 head.next 为起点,需要反转前 n - 1 个节点
        ListNode last = reverse(head.next,n-1); 
        head.next.next = head; //逆转
        head.next = temp; //让反转之后的 head 节点和后面的节点连起来
        return last;
     }
}

四、k个一组反转链表

ListNode reverseKGroup(ListNode head, int k) {
    if (head == null) return null;
    // 区间 [a, b) 包含 k 个待反转元素
    ListNode a, b;
    a = b = head;
    for (int i = 0; i < k; i++) {
        // 不足 k 个,不需要反转,base case
        if (b == null) return head;
        b = b.next;
    }
    // 反转前 k 个元素
    ListNode newHead = reverse(a, b);
    // 递归反转后续链表并连接起来
    a.next = reverseKGroup(b, k);
    return newHead;
}
 // 反转区间 [a, b) 的元素,注意是左闭右开  
ListNode reverse(ListNode a, ListNode b) {
    ListNode pre, cur, nxt;
    pre = null; cur = a; nxt = a;
    // while 终止的条件改一下就行了
    while (cur != b) {
        nxt = cur.next;
        cur.next = pre;
        pre = cur;
        cur = nxt;
    }
    // 返回反转后的头结点
    return pre;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值