代码随想录算法训练营第四天| 24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题 02.07. 链表相交 142.环形链表II()

24. 两两交换链表中的节点

class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummyHead = new ListNode();
        dummyHead.next = head;
        ListNode cur = dummyHead;
        while (cur.next != null && cur.next.next != null) {
            ListNode tmp;
            tmp = cur.next.next;
            cur.next.next = tmp.next;
            tmp.next = cur.next;
            cur.next = tmp;
            cur = tmp.next;
        }
        return dummyHead.next;
    }
}

19.删除链表的倒数第N个节点

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyHead = new ListNode();
        dummyHead.next = head;
        ListNode slow = dummyHead;
        ListNode fast = dummyHead;
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummyHead.next;
    }
}

面试题 02.07. 链表相交

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode Ahead = headA;
        ListNode Bhead = headB;

        while (Ahead != Bhead) {
            if (Ahead == null) {
                Ahead = headB;
            } else {
                Ahead = Ahead.next;
            }
            if (Bhead == null) {
                Bhead = headA;
            } else {
                Bhead = Bhead.next;
            }
        }
        return Ahead;
    }
}

142. 环形链表 II

public class Solution {
    public ListNode detectCycle(ListNode head) {
        boolean isMeet = false;
        
        if (head == null ||head.next == null) {
            return null;
        }
        ListNode fast = head.next.next, slow = head.next;
        if (fast == head) {
            return head;
        }
        while (fast != slow && fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                isMeet = true;
            }
        }
        if (isMeet) {
            slow = head;
            while (slow != fast) {
                slow = slow.next;
                fast = fast.next;
            }
        }else{
            fast = null;
        }
        return fast;
        
    }
}
————————

24. 两两交换链表中的节点

class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummyHead = new ListNode();
        dummyHead.next = head;
        ListNode cur = dummyHead;
        while (cur.next != null && cur.next.next != null) {
            ListNode tmp;
            tmp = cur.next.next;
            cur.next.next = tmp.next;
            tmp.next = cur.next;
            cur.next = tmp;
            cur = tmp.next;
        }
        return dummyHead.next;
    }
}

19.删除链表的倒数第N个节点

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyHead = new ListNode();
        dummyHead.next = head;
        ListNode slow = dummyHead;
        ListNode fast = dummyHead;
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummyHead.next;
    }
}

面试题 02.07. 链表相交

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode Ahead = headA;
        ListNode Bhead = headB;

        while (Ahead != Bhead) {
            if (Ahead == null) {
                Ahead = headB;
            } else {
                Ahead = Ahead.next;
            }
            if (Bhead == null) {
                Bhead = headA;
            } else {
                Bhead = Bhead.next;
            }
        }
        return Ahead;
    }
}

142. 环形链表 II

public class Solution {
    public ListNode detectCycle(ListNode head) {
        boolean isMeet = false;
        
        if (head == null ||head.next == null) {
            return null;
        }
        ListNode fast = head.next.next, slow = head.next;
        if (fast == head) {
            return head;
        }
        while (fast != slow && fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                isMeet = true;
            }
        }
        if (isMeet) {
            slow = head;
            while (slow != fast) {
                slow = slow.next;
                fast = fast.next;
            }
        }else{
            fast = null;
        }
        return fast;
        
    }
}