链表+双指针+分治法

牛客网 BM5合并K个已排序链表
首先写出和并两个已排序链表的方法,然后用分治法合并k个链表

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param lists ListNode类ArrayList 
     * @return ListNode类
     */
    public ListNode mergeKLists (ArrayList<ListNode> lists) {
        return mergeKListNodes(lists, 0, lists.size()-1);
    }
    ListNode mergeKListNodes(ArrayList<ListNode> lists,int n, int m){//分治法
        if(n > m)return  null;
        else if(n == m) return lists.get(n);
        int mid = (n + m)/2;
        return mergeListNode(mergeKListNodes(lists, n, mid), mergeKListNodes(lists, mid + 1, m));
    }
    public ListNode mergeListNode(ListNode pHead1, ListNode pHead2){
        ListNode ans = new ListNode(-1);
        ListNode pre1 = pHead1;
        ListNode pre2 = pHead2;
        ListNode pre = ans;
        while(pre1 != null && pre2 != null){
            if(pre1.val < pre2.val){
                pre.next = pre1;
                pre1 = pre1.next;
                pre = pre.next;
            }
            else{
                pre.next = pre2;
                pre2 = pre2.next;
                pre = pre.next;
            }
        }
        if(pre1 != null){
            pre.next = pre1;
        }
        else{
            pre.next = pre2;
        }
        return ans.next;
    }
}

BM6 判断链表是否有环
法一:双指针

import java.util.*;
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null) return false;
        ListNode slow = head;
        ListNode fast = head;
        while(slow != null && fast != null){
            slow = slow.next;
            if(fast.next != null){
                fast = fast.next.next;
            }else return false;
            if(slow == fast){
                return true;
            }
        }
        return false;
    }
}

法二:set集合

import java.util.*;
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode ans = head;
        Set<ListNode> node = new HashSet<ListNode>();
        while(ans != null){
            if(node.contains(ans)){
                return true;
            }
            node.add(ans);
            ans = ans.next;
        }
        return false;
    }
}

BM7链表中环的入口
法一:hashset第一个contatins();
法二:快慢指针如果到环结点的距离为k,环的长度为l。当slow点到达结点,fast离结点为k,两者相差l-k。则经过l-k后两者相聚,s离结点l-k,经过k步到达结点。————相聚后令fast=head,第一个相遇点及入口。

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值