zoukankan      html  css  js  c++  java
  • 链表有环判断,快慢指针两种方法/合并链表/删除重复元素/二分递归和while

        public static boolean hasCycle(ListNode head) {
            if (head == null || head.next == null) {
                return false;
            }
            ListNode slow = head;
            ListNode fast = head.next;
            while (slow != fast) {
                if (slow.next == null)
                    return false;
                slow = slow.next;
                if (fast.next == null)
                    return false;
                if (fast.next.next == null)
                    return false;
                fast = fast.next.next;
            }
            return true;
        }
        public static boolean hasCycle1(ListNode head) {
            if (head == null || head.next == null) {
                return false;
            }
            ListNode slow = head;
            ListNode fast = head.next;
            while (fast.next != null) {
                if (slow == fast) {
                    return true;
                }
                slow = slow.next;
                if (fast.next.next == null){
                    return false;
                }
                fast = fast.next.next;
            }
            return false;
        }
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            ListNode result = new ListNode(0);
            ListNode head1 = l1;
            ListNode head2 = l2;
            ListNode resultPoint = result;
            while (head1 != null && head2 != null) {
                if (head1.val <= head2.val) {
                    ListNode currNode1 = new ListNode(head1.val);
                    resultPoint.next = currNode1;
                    resultPoint = resultPoint.next;
                    head1 = head1.next;
                } else {
                    ListNode currNode2 = new ListNode(head2.val);
                    resultPoint.next = currNode2;
                    resultPoint = resultPoint.next;
                    head2 = head2.next;
                }
            }
            if (head1 != null) {
                resultPoint.next = head1;
            }
            if (head2 != null) {
                resultPoint.next = head2;
            }
            return result.next;
        }
    }
        public static ListNode deleteDuplicates(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode pre = head;
            ListNode curr = head.next;
            while (curr != null) {
                if (curr.val == pre.val) {
                    pre.next = curr.next;
                } else {
                    pre = pre.next;
                }
                curr = curr.next;
            }
            return head;
        }
    public static boolean binarySearchDigui(int[] array, int start, int end, int val){
            if (start >= end) {
                return false;
            }
            int mid = start + (end - start) / 2;
            if (val < array[mid]) {
                return binarySearchDigui(array, start, mid, val);
            } else if (val > array[mid]){
                return binarySearchDigui(array, mid + 1, end, val);
            } else {
                return true;
            }
        }
        public static boolean binarySearchWhile(int[] array, int start, int end, int val){
            while (start < end) {
                int mid = start + (end - start) / 2;
                if (val < array[mid]) {
                    end = mid;
                } else if (val > array[mid]){
                    start = mid + 1;
                } else {
                    return true;
                }
            }
            return false;
        }
  • 相关阅读:
    String前后去掉空格、option只能定义value值吗?还能添加什么值。dom和Jquery对象转换。
    JsonObject没有fromObject、idea引入maven有红线没依赖、JsonObject maven 依赖包
    json几个小例子
    [极客大挑战 2019]PHP1
    栈溢出原理笔记(一)
    CentOS7怎么安装图形界面
    快速搭建WordPress博客
    Mac 安装Mysql 之 Sqlservice 区别
    Metasploit-初篇
    Windows To Go 制作详解
  • 原文地址:https://www.cnblogs.com/it-worker365/p/6961342.html
Copyright © 2011-2022 走看看