代码随想录算法训练营Day4|链表part2

本文介绍了如何在链表中进行两两交换节点、删除倒数第N个节点、解决链表相交问题以及检测环形链表,包括递归和非递归方法。
摘要由CSDN通过智能技术生成

代码随想录算法训练营Day4|链表part2



题目

    1. 两两交换链表中的节点
  • 19.删除链表的倒数第N个节点
  • 面试题 02.07. 链表相交
  • 142.环形链表II

一,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) {
        //设置虚拟头节点且指向头节点
        ListNode dumHead=new ListNode(-1);
        dumHead.next=head;

        ListNode cur = dumHead;
         while(cur.next!=null&&cur.next.next!=null)
        //当链表为偶/奇数个,遍历到后面为空时停止
        {
            ListNode temp=cur.next;
            ListNode temp1=cur.next.next.next;
            cur.next=cur.next.next;
            cur.next.next=temp;
            temp.next=temp1;
            cur=cur.next.next;
        
        }
        return dumHead.next;
    }
}

递归版(来自代码随想录)

// 递归版本
class Solution {
    public ListNode swapPairs(ListNode head) {
        // base case 退出提交
        if(head == null || head.next == null) return head;
        // 获取当前节点的下一个节点
        ListNode next = head.next;
        // 进行递归
        ListNode newNode = swapPairs(next.next);
        // 这里进行交换
        next.next = head;
        head.next = newNode;

        return 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) {
        ListNode newHead=new ListNode(0);
        newHead.next=head;
        ListNode slow=newHead;
        ListNode fast=newHead;     
        
        //将快指针向后移动与满指针间隔n-1个位置
        //当快指针指向null,慢指针与其间隔n个,则慢指针正好指向要删除的节点
        //这样当快指针指向空的时候,满指针指向的是要删除的节点的前一个位置
      for(int i=0;i<n;i++){
            //快慢指针间隔n-1,只要满足这个条件,慢指针就可以正确指向
            fast=fast.next;
        }
        while(fast.next!=null){
            //快慢指针同时移动
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return newHead.next;
    }
}

三、面试题 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) {
        ListNode cruA=headA;
        ListNode cruB=headB;
        //Len值链表长度
        int LenA=0;
        int LenB=0;
        while(cruA!=null){
            LenA++;
            cruA=cruA.next;
        }
        while(cruB!=null){
            LenB++;
            cruB=cruB.next;
        }
         cruA=headA;
         cruB=headB;

        if(LenB>LenA){
            int temp=LenB;
            LenB=LenA;
            LenA=temp;
            ListNode tempC=cruB;
            cruB=cruA;
            cruA=tempC;
        }
        //求长度差
        int length=LenA-LenB;
        //让两个指针分别在单链表,使之末尾对齐的状态
        while(length-- >0){
            cruA=cruA.next;
        }
        while(cruA!=null){
            if(cruA==cruB){
                return cruA;
            }
            cruA=cruA.next;
            cruB=cruB.next;
            
        }
        return null;
    }
}

四、142.环形链表II

应该可以用hash表来做,but是链表的章节
代码随想录里面有详细的图解
重要的是Z的大小,决定了入口处

/**
 * 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) {
         ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {// 相遇
                ListNode index1 = fast;
                ListNode index2 = head;
                // 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
                while (index1 != index2) {
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值