代码随想录算法训练营DAY4|24、19、142、面试0207

24两两交换链表中的节点

题目链接:24. 两两交换链表中的节点 - 力扣(LeetCode)

难点:设置基准点,本题的基准点是要交换的两个节点前一个节点。要注意赋值顺序,利用变量保存节点。 

/**
 * 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 dummyhead=new ListNode();
        dummyhead.next=head;
        ListNode cur=dummyhead;
        while(cur.next!=null&&cur.next.next!=null){
            ListNode pre=cur.next;
            ListNode last=cur.next.next.next;
            cur.next=cur.next.next;
            cur.next.next=pre;
            pre.next=last;
            cur=cur.next.next;
        }
        return dummyhead.next;
    }
}

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

题目链接:19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

难点:运用快慢指针,让快指针先走N+1步,之后快慢指针同时向前。本题的基准点是倒数第N+1个结点。 

/**
 * 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 dummyhead=new ListNode();
        dummyhead.next=head;
        ListNode fast=dummyhead;
        ListNode slow=dummyhead;
        int loop=n+1;
        while(fast!=null&&loop-->0){
            fast=fast.next;
        }
        while(fast!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return dummyhead.next;
    }
}

142环形链表

题目链接:142. 环形链表 II - 力扣(LeetCode)

难点:运用快慢指针。快指针走两步,慢指针走一步,当 快慢指针相遇时,表示存在环。此时再设置一个指针指向头节点,与慢指针以相同步数前进,再次相遇时即是环的起始。

/**
 * 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 fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                ListNode index=head;
                while(slow!=index){
                    slow=slow.next;
                    index=index.next;
                }
                return index;
            }
        }
        return null;
    }
}

面试题0207链表相交

题目链接:面试题 02.07. 链表相交 - 力扣(LeetCode)

思路:找出更长的一组,设置两个指针分别指向两个链表,让长的链表先走len=lenA-lenB步,之后两个指针同时移动,两个指针所指节点相同的既是相交的第一个节点。

/**
 * 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) {
        if(headA==null||headB==null){
            return null;
        }
        ListNode curA=headA;
        ListNode curB=headB;
        int lenA=0;
        int lenB=0;
        int gap=0;
        while(curA!=null){
            curA=curA.next;
            lenA++;
        }
        while(curB!=null){
            curB=curB.next;
            lenB++;
        }
        // 让指针重新指向头节点
        curA=headA;
        curB=headB;
        if(lenA<lenB){
            int templ=lenA;
            lenA=lenB;
            lenB=templ;
            ListNode temp=curA;
            curA=curB;
            curB=temp;
        }
        gap=lenA-lenB;
        while(gap-->0){
            curA=curA.next;
        }
        while(curA!=null){
            if(curA==curB){
                return curA;
            }
            curA=curA.next;
            curB=curB.next;
        }
        return null;
    }
}
/**
 * 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) {
        if(headA==null||headB==null){
            return null;
        }
        ListNode curA=headA;
        ListNode curB=headB;
        while(curA!=curB){
            curA=curA==null?headB:curA.next;
            curB=curB==null?headA:curB.next;
        }
        return curA;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值