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

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

/**
 * 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 dummy = new ListNode(0);

        dummy.next = head;


        ListNode current = dummy;

        while(current.next!= null && current.next.next != null){
            ListNode first = current.next;
            ListNode second = current.next.next;

            current.next = second;
            first.next = second.next;
            second.next = first;

            current = current.next.next;
        }

        return dummy.next;


    }
}

 

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

/**
 * 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) {
        if(head == null){
            return head;
        }
        int length  = 1;
        ListNode current = head;
        while(current.next!= null){
            length++;
            current = current.next;
        }
        System.out.println(length);
        if(length == n){
            return head.next;
        }
        ListNode p = head;
        for(int i = 1; i <= length - n -1; i++){
            p = p.next;
        }
        p.next = p.next.next;
        return head;
    }
}
  1. 如果头结点head为空,直接返回,因为一个空链表没有节点可以删除。

  2. current指针遍历链表,一边遍历一边计数,得到链表的总长度length

  3. 如果链表的长度恰好等于n,说明要删除的是头结点。因此,返回head.next即可,此时原链表的第二个节点成为了新链表的头结点。

  4. 如果要删除的不是头结点,那么再次从头结点开始,遍历链表。这次只遍历到“第(length - n - 1)个节点”,这个节点就是要删除节点的前一个节点。将这个节点的next指向next.next,就相当于删除了下一个节点。

  5. 最后返回头结点head

面试题 02.07. 链表相交

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        // 两个指针分别从头节点headA headB 开始遍历
        // 当一个指针到达链表末尾时 就将它已到另一个链表的头部继续遍历
        // 这样两个指针会相遇在相交节点

        if(headA == null ||  headB == null){
            return null;
        }

        ListNode pA = headA;
        ListNode pB = headB;

        while(pA != pB){
            if(pA == null){
                pA = headB;
            } else {
                pA = pA.next;
            }

            if(pB == null){
                pB = headA;
            } else {
                pB = pB.next;
            }
        }

        return pA;
        
    }
}

142. 环形链表 II

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        // 检查边界
        if(head == null || head.next == null){
            return null;
        }

        // 初始化慢指针和快指针
        ListNode slow = head;
        ListNode fast = head;
        // floyd算法 找到相遇点
        while( fast != null && fast.next != null){
            // 慢指针移动一步
            slow = slow.next;
            // 快指针移动两步
            fast = fast.next.next;
            
            // 如果慢指针和快指针相遇,说明存在环
            if(slow == fast){
                break;
            }
        }

        //  如果不存在环,返回null
        if( fast == null || fast.next == null){
            return null;
        }

        // 将慢指针移回链表头部, 寻找环的入口
        // 当慢指针和快指针再次相遇时,相遇点即为环的入口
        slow = head;
        while(slow != fast){
            slow = slow.next;
            fast = fast.next;
        }

        // 返回环的入口节点
        return slow;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第二十二天的算法训练主要涵盖了Leetcode题目的三道题目,分别是Leetcode 28 "Find the Index of the First Occurrence in a String",Leetcode 977 "有序数组的平方",和Leetcode 209 "长度最小的子数组"。 首先是Leetcode 28题,题目要求在给定的字符串找到第一个出现的字符的索引。思路是使用双指针来遍历字符串,一个指向字符串的开头,另一个指向字符串的结尾。通过比较两个指针所指向的字符是否相等来判断是否找到了第一个出现的字符。具体实现的代码如下: ```python def findIndex(self, s: str) -> int: left = 0 right = len(s) - 1 while left <= right: if s[left == s[right]: return left left += 1 right -= 1 return -1 ``` 接下来是Leetcode 977题,题目要求对给定的有序数组的元素进行平方,并按照非递减的顺序返回结果。这里由于数组已经是有序的,所以可以使用双指针的方法来解决问题。一个指针指向数组的开头,另一个指针指向数组的末尾。通过比较两个指针所指向的元素的绝对值的大小来确定哪个元素的平方应该放在结果数组的末尾。具体实现的代码如下: ```python def sortedSquares(self, nums: List[int]) -> List[int]: left = 0 right = len(nums) - 1 ans = [] while left <= right: if abs(nums[left]) >= abs(nums[right]): ans.append(nums[left ** 2) left += 1 else: ans.append(nums[right ** 2) right -= 1 return ans[::-1] ``` 最后是Leetcode 209题,题目要求在给定的数组找到长度最小的子数组,

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值