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

24: 第一眼看没什么思路,就直接去看文档了,主要掌握的知识是如何设置虚拟节点,这样非常方便操作。只需要设置一个新的Node,然后把他放到head的前面,就算是新的虚拟节点了。在while loop里面要注重每一步的逻辑顺序(画图最为清晰)。在这里设置两个temp主要是为了保留原来的值,这样swap就不会搞错了。

Code:

/**
 * 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 temp = current.next;
            ListNode temp1 = current.next.next.next;
            //step 1
            current.next = current.next.next;
            //step 2
            current.next.next = temp;
            //step 3
            current.next.next.next = temp1;
            //switch the current position
            current = current.next.next;
        }
        return dummy.next;
    }
}

用时:15min

19: 第一个思路大概就是用length-n算出到底要remove哪一个index的数值然后再用正常的remove, 但是发现只给了head,没办法找到length。看了提示之后发现要用两个pointer,于是想出来一个pointer先走n,然后两个在一起移动,直到先移动的pointer.next == null。然后第二个pointer就定位到了要移动的index的前一位。但是在测试的时候发现要加一个dummy node,这样可以避免要移走第一个index时候的麻烦。

Code:

/**
 * 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;
        if(head == null){
            return head;
        }else if(head.next == null && n == 1){
            return null;
        }
        ListNode first = dummy;
        ListNode second = dummy;
        for(int i=0; i<n; i++){
            second = second.next;
        }
        while(second.next != null){
            first = first.next;
            second = second.next;
        }
        first.next = first.next.next;
        return dummy.next;
    }
}

160: 主要思路是要找出长度差,然后屁股对齐,这样就能看出来重合的pointer。注意要用ptr去指代head,要不然会乱。

Code: 

/**
 * 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 lengthA = 0;
        ListNode ptrA = headA;
        while(ptrA != null){
            ptrA = ptrA.next;
            lengthA++;
        }
        int lengthB = 0;
        ListNode ptrB = headB;
        while(ptrB != null){
            ptrB = ptrB.next;
            lengthB++;
        }
        int diff = Math.abs(lengthA-lengthB);
        ptrA = headA;
        ptrB = headB;
        if(lengthA > lengthB){
            for(int i=0; i< diff; i++){
                ptrA = ptrA.next;
            }
        }else{
            for(int i=0; i< diff; i++){
                ptrB = ptrB.next;
            }
        }

        while(ptrA != null){
            if(ptrA != ptrB){
                ptrA = ptrA.next;
                ptrB = ptrB.next;
            }else{
                return ptrA;
            }
        }
        return null;
    }
}

142: 主要是思路要想明白,多回顾思路

Code:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值