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

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

原题链接: 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 nhead = new ListNode(0);
        ListNode ptr1, ptr2;
        nhead.next = head;
        if (head == null)
            return head;
        if (head.next == null)
            return head;
        ptr1 = head;
        ptr2 = head.next;
        ListNode lastnode = nhead;
        while (ptr2 != null) {
            ptr1.next = ptr2.next;
            ptr2.next = ptr1;
            lastnode.next = ptr2;
            lastnode = lastnode.next;
            ptr2 = ptr1.next;
            ptr1 = lastnode.next;
            if (ptr2 == null)
                break;
            ptr2 = ptr2.next;
            ptr1 = ptr1.next;
            lastnode = lastnode.next;
        }
        return nhead.next;
    }
}

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

原题链接: 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 ptr1, ptr2;
        ListNode nhead = new ListNode(0);
        nhead.next = head;
        ptr1 = nhead;
        ptr2 = nhead;
        for (int i = 0; i < n; i++) {
            ptr2 = ptr2.next;
        }
        while (ptr2.next != null) {
            ptr1 = ptr1.next;
            ptr2 = ptr2.next;
        }
        ptr1.next = ptr1.next.next;
        return nhead.next;
    }
}

面试题 02.07. 链表相交

原题链接: 面试题 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) {
        int lenA=0;
        int lenB=0;
        ListNode ptr1 = headA;
        ListNode ptr2 = headB;
        int dif=0;
        if(headA==null||headB==null)
            return null;
        while(ptr1.next != null){
            ptr1 = ptr1.next;
            lenA++;
        }
        while(ptr2.next != null){
            ptr2 = ptr2.next;
            lenB++;
        }
        if(lenA>lenB){
            dif = lenA - lenB;
            ptr1 = headA;
            ptr2 = headB;
            for(int i=0;i<dif;i++){
                ptr1=ptr1.next;
            }
            while(ptr1.next!=null){
                if(ptr1==ptr2){
                    return ptr1;
                }
                ptr1=ptr1.next;
                ptr2=ptr2.next;
            }

        }else{
            dif = lenB - lenA;
            ptr1 = headA;
            ptr2 = headB;
            for(int i=0;i<dif;i++){
                ptr2=ptr2.next;
            }
            while(ptr2.next!=null){
                if(ptr1==ptr2){
                    return ptr2;
                }
                ptr1=ptr1.next;
                ptr2=ptr2.next;
            }
        }
        if(ptr1==ptr2)
            return ptr1;
        return null;
    }
}

142.环形链表II

原题链接: 142.环形链表II
解题思路:

  1. 使用快慢指针确定是否有环+相遇节点位置
  2. 将其中一个指针指回头节点,然后两个指针每次只走一个节点,再次相遇位置即为入环节点处
/**
 * 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 ptr1, ptr2;
        if (head == null)
            return null;
        if (head.next == null)
            return null;
        ptr1 = head;
        ptr2 = head;
        while (ptr2.next != null) {
            ptr1 = ptr1.next;
            ptr2 = ptr2.next;
            if (ptr2.next == null)
                return null;
            ptr2 = ptr2.next;
            if (ptr1 == ptr2)
                break;
        }
        if (ptr2.next == null)
            return null;
        ptr1 = head;
        while (ptr1 != ptr2) {
            ptr1 = ptr1.next;
            ptr2 = ptr2.next;
        }
        return ptr1;
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值