环链表入口_翻转链表_合并有序链表_树的子结构

23剑指Offer_23_链表中环的入口结点

/**
 * https://leetcode-cn.com/problems/linked-list-cycle-ii/
 *
 * @author Qitong!!
 * @Date 2020/7/22
 */


public class 剑指Offer_23_链表中环的入口结点 {
    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null || head.next.next == null) return null;
        ListNode slow = head.next;
        ListNode fast = head.next.next;
        //相遇
        while (slow != fast) {
            if (fast.next == null || fast.next.next == null) {
                return null;
            }
            slow = slow.next;
            fast = fast.next.next;

        }
        //相遇后将快指针指向头结点继续!
        fast = head;

        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
}

24 剑指Offer_24_反转链表

/**
 * https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
 *
 * @author Qitong!!
 * @Date 2020/7/2
 */
public class 剑指Offer_24_反转链表 {
    //递归 时间O(N)  空间 O(N)
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;

        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }


    //遍历一遍!时间O(N) 空间 O(1)
    public ListNode reverseList2(ListNode head) {
        if (head == null || head.next == null) return head;

        ListNode newHead = null;
        while (head != null) {
            ListNode tmp = head.next;
            head.next = newHead;
            newHead = head;
            head = tmp;
        }
        return newHead;
    }

25 剑指Offer_25_合并两个排序的链表

/**
 * https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/
 *
 * @author Qitong!!
 * @Date 2020/7/2
 */
public class 剑指Offer_25_合并两个排序的链表 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        ListNode dummyHead = new ListNode(0);
        ListNode dummyTail = dummyHead;
        while (l1 != null && l2 != null) {
            if (l1.val >= l2.val) {
                dummyTail.next = l2;
                l2 = l2.next;
            } else {
                dummyTail.next = l1;
                l1 = l1.next;
            }
            dummyTail = dummyTail.next;
        }
        if (l1 != null) {
            dummyTail.next = l1;
        }
        if (l2 != null) {
            dummyTail.next = l2;
        }
        return dummyHead.next;
    }
}

26 剑指Offer_26_树的子结构

/**
 * https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/
 *
 * @author Qitong!!
 * @Date 2020/7/2
 */
public class 剑指Offer_26_树的子结构 {
    //子结构 , 不是 子树!!
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null || B == null) return false;

        return recur(A,B)||isSubStructure(A.left,B)||isSubStructure(A.right,B);
    }
    private boolean recur(TreeNode A,TreeNode B){
        if (B == null) return true;
        if (A == null || A.val != B.val) return false;

        return recur(A.left, B.left)&&recur(A.right,B.right);
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_45399846/article/details/107558546