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

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
 // 递归解法
class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode rest = head.next.next;
        ListNode newHead = head.next;
        newHead.next = head;
        head.next = swapPairs(rest);
        return newHead;
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
 // 虚拟头节点
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        ListNode prev = dummyNode;

        // 只有当prev指向的结点之后有两个结点时才会进行交换
        while (prev.next != null && prev.next.next != null) {
            ListNode temp = head.next.next // 把next存在临时节点中
            prev.next = head.next; 
            head.next.next = head; 
            head.next = temp; 
            prev = head; // 进1位
            head = head.next; // 进1位
        }
        return dummyNode.next;
    }
}

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        // 对所删除节点的前一个节点进行操作
        ListNode dummyNode = new ListNode(0, head);
        ListNode fastPointer = dummyNode;
        ListNode slowPointer = dummyNode;

        // 让快指针运动n步
        while(n > 0) {
            fastPointer = fastPointer.next;
            n--;
        }

        while (fastPointer.next != null) {
            fastPointer = fastPointer.next;
            slowPointer = slowPointer.next;
        }
        
        // 此时慢指针的位置是, 要删除的前一个节点, 所以要删除当前节点的下一个节点
        slowPointer.next = slowPointer.next.next;
        return dummyNode.next;
    }
}

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
 //ref. leetcode
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode currentA = headA;
        ListNode currentB = headB;
        int lenA = 0;
        int lenB = 0;

        while (currentA != null) { // 求链表A的长度
            currentA = currentA.next;
            lenA++;
        }

        while (currentB != null) { // 求链表B的长度
            currentB = currentB.next;
            lenB++;
        }

        currentA = headA;
        currentB = headB;

        if (lenB > lenA) {
            //1. swap (lenA, lenB);
            int tmpLen = lenA;
            lenA = lenB;
            lenB = tmpLen;

            //2. swap (currentA, currentB);
            ListNode tmpNode = currentA;
            currentA = currentB;
            currentB = tmpNode;
        }

        // 求长度差值
        int gap = lenA - lenB;

        // 让currentA和currentB的末尾对齐
        while (gap-- > 0) {
            currentA = currentA.next;
        }

        // 遍历currentA 和 currentB,如果不相同, 同时向后移动currentA和currentB, 如果相同则直接返回
        while (currentA != null) {
            if (currentA == currentB) {
                return currentA;
            }
            currentA = currentA.next;
            currentB = currentB.next;
        }
        return null;
    }
}

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
 // 走a+nb时候一定是在环的入口
 // 第一次相遇的时候慢指针走了nb步
public class Solution {
    public ListNode detectCycle(ListNode head) {
        // 双指针问题, 有环意味着快慢指针会相遇
        // 快指针每次走两个结点, 慢指针每次走一个节点
        ListNode fast = head; 
        ListNode slow = head; 
        while(true) {
            if (fast == null || fast.next == null) {
                return null;
            } else {
                fast = fast.next.next;
                slow = slow.next;
                if (fast == slow) {
                    break;
                }
            }
        }
        fast = head;
        while(slow != fast) {
            slow = slow.next;
            fast = fast.next; 
        }
        return fast;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值