代码随想录算法训练营Day4

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 dummy=new ListNode(0);
        dummy.next=head;
        ListNode cur=dummy;
        ListNode firstnode=null;
        ListNode secondnode=null;
        ListNode temp=null;
        while(cur.next!=null&&cur.next.next!=null){
            temp=cur.next.next.next;
            firstnode=cur.next;
            secondnode=cur.next.next;
            cur.next=secondnode;
            secondnode.next=firstnode;
            firstnode.next=temp;
            cur=firstnode;
        }
        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) {
    ListNode dummy=new ListNode(0);
    dummy.next=head;
    ListNode leftnode=dummy;
    ListNode rightnode=dummy;
    for(int i=0;i<=n;i++){
        rightnode=rightnode.next;
    }
    while(rightnode!=null){
        leftnode=leftnode.next;
        rightnode=rightnode.next;
    }
    leftnode.next=leftnode.next.next;
    return dummy.next;

    }
}

160.链表相交

双指针同步

/**
 * 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 curA=headA;
        ListNode curB=headB;
        int lengthA=0;
        int lengthB=0;
        while(curA!=null){
            lengthA++;
            curA=curA.next;
        }
        while(curB!=null){
            lengthB++;
            curB=curB.next;
        }
        curA=headA;
        curB=headB;
        if(lengthA>lengthB){
            int gap=lengthA-lengthB;
            for(int i=0;i<gap;i++){
                curA=curA.next;
            }
            while(curA!=null){
                if(curA==curB){
                    return curA;
                }
                curA=curA.next;
                curB=curB.next;
            }
            return null;
        }else{
            int gap=lengthB-lengthA;
            for(int i=0;i<gap;i++){
                curB=curB.next;
            }
            while(curB!=null){
                if(curA==curB){
                    return curB;
                }
                curA=curA.next;
                curB=curB.next;
            }
            return null;
        }
    }
}

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) {

        ListNode fastnode=head;
        ListNode slownode=head;
        while(fastnode!=null&&fastnode.next!=null){
            slownode=slownode.next;
            fastnode=fastnode.next.next;
            if(slownode==fastnode){
                ListNode index1=fastnode;
                ListNode index2=head;
                while(index1!=index2){
                    index1=index1.next;
                    index2=index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值