牛客 TOP101(1~5)

1、反转链表

在这里插入图片描述

public class Solution {
    public ListNode ReverseList(ListNode head) {
        // 迭代(头插法)
        if (head == null)
            return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode p = head;
        // 将 p 后面的节点头插到 dummy 后面
        while (p.next != null) {
            ListNode moved = p.next;
            p.next = p.next.next;
            moved.next = dummy.next;
            dummy.next = moved;
        }
        return dummy.next;
    }
}
public class Solution {
    public ListNode ReverseList(ListNode head) {
        // 递归
        if (head == null || head.next == null)
            return head;
        ListNode node = ReverseList(head.next);
        head.next.next = head;
        head.next = null;
        return node;
    }
}

2、链表内指定区间反转

在这里插入图片描述

public class Solution {
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // 迭代(头插法)
        if (head == null || head.next == null || m == n)
            return head;
        ListNode dummy = new ListNode(-1001);
        dummy.next = head;
        ListNode g = dummy;
        int i = 1;
        while (i < m) {
            g = g.next;
            i++;
        }
        ListNode p = g.next;
        // 将 p 后面的节点头插到 g 后面
        for (int step = 1; step <= n - m; step++) {
            ListNode moved = p.next;
            p.next = p.next.next;
            moved.next = g.next;
            g.next = moved;
        }
        return dummy.next;
    }
}
public class Solution {
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // 递归
        if (m == 1)
            return reverseLeftN(head, n);
        ListNode between = reverseBetween(head.next, m - 1, n - 1);
        head.next = between;
        return head;
    }
    
    ListNode successor = null;    // 反转前 N 个节点后的后继节点
    // 反转链表的前 N 个节点
    public ListNode reverseLeftN(ListNode head, int n) {
        if (n == 1) {
            successor = head.next;
            return head;
        }
        ListNode node = reverseLeftN(head.next, n - 1);
        head.next.next = head;
        head.next = successor;
        return node;
    }
}

3、链表中的节点每 k 个一组翻转

在这里插入图片描述

public class Solution {
    public ListNode reverseKGroup (ListNode head, int k) {
        // 迭代(头插法)
        if (head == null || head.next == null || k == 1)
            return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        // 计算链表长度,以确定翻转次数
        int length = 0;
        ListNode cur = head;
        while (cur != null) {
            length++;
            cur = cur.next;
        }
        // 翻转 length / k 次
        ListNode g = dummy, p = head;
        for (int i = 0; i < length / k; i++) {
            reverseBetween(g, p, k);
            g = p;
            p = p.next;
        }
        return dummy.next;
    }
    
    public void reverseBetween(ListNode g, ListNode p, int k) {
        for (int step = 1; step < k; step++) {
            ListNode moved = p.next;
            p.next = p.next.next;
            moved.next = g.next;
            g.next = moved;
        }
    }
}
public class Solution {
    public ListNode reverseKGroup (ListNode head, int k) {
        // 递归
        if (head == null || head.next == null || k == 1)
            return head;
        ListNode tail = head;
        for (int i = 0; i < k; i++) {
            // 剩余数量小于 k 的话,不需要翻转
            if (tail == null)
                return head;
            tail = tail.next;
        }
        
        // 翻转 [head, tail) 的链表
        ListNode node = reverse(head, tail);
        
        head.next = reverseKGroup(tail, k);
        return node;
    }
    
    // 翻转 [head, tail) 的链表
    public ListNode reverse(ListNode head, ListNode tail) {
        ListNode prev = null, cur = null;
        while (head != tail) {
            ListNode next = head.next;
            head.next = prev;
            prev = head;
            head = next;
        }
        return prev;
    }
}

4、合并两个排序的链表

在这里插入图片描述

public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        // 双指针
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                cur.next = list1;
                list1 = list1.next;
            } else {
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        cur.next = list1 != null ? list1 : list2;
        return dummy.next;
    }
}
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        // 递归
        if (list1 == null)
            return list2;
        if (list2 == null)
            return list1;
        if (list1.val <= list2.val) {
            list1.next = Merge(list1.next, list2);
            return list1;
        } else {
            list2.next = Merge(list1, list2.next);
            return list2;
        }
    }
}

5、合并 k 个已排序的链表

在这里插入图片描述

class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        // 归并排序
        if (lists == null || lists.length == 0)
            return null;
        int length = lists.length;
        if (length == 1)
            return lists[0];
        if (length == 2)
            return mergeTwoLists(lists[0], lists[1]);
        
        ListNode[] left = new ListNode[(length + 1) / 2];
        ListNode[] right = new ListNode[length / 2];
        int i = 0;
        for (; i < (length + 1) / 2; i++)
            left[i] = lists[i];
        int j = 0;
        for (; i < length; i++)
            right[j++] = lists[i];
        
        return mergeTwoLists(mergeKLists(left), mergeKLists(right));
    }

    private ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null)
            return list2;
        if (list2 == null)
            return list1;
        
        ListNode dummy = new ListNode();
        ListNode cur = dummy;
        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                cur.next = list1;
                list1 = list1.next;
            } else {
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        cur.next = list1 != null ? list1 : list2;
        return dummy.next;
    }
}
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        // 优先队列
        if (lists == null || lists.length == 0)
            return null;
        if (lists.length == 1)
            return lists[0];
        PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> Integer.compare(a.val, b.val));
        for (ListNode list : lists) {
            if (list != null)
                pq.offer(list);
        }

        ListNode dummy = new ListNode();
        ListNode cur = dummy;
        while (!pq.isEmpty()) {
            ListNode nextNode = pq.poll();
            cur.next = nextNode;
            cur = cur.next;
            if (nextNode.next != null) {
                pq.offer(nextNode.next);
            }
        }

        return dummy.next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值