刷题训练day4 | 第二章链表 part02

题目1:【二刷完成】

代码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 swapPairs(ListNode head) {
       ListNode dummyhead = new ListNode(-1);
       dummyhead.next = head;
       ListNode cur = dummyhead;
       ListNode temp1 = null;
       ListNode temp2 = null;
       ListNode temp3 = null;
       while(cur.next != null && cur.next.next != null){
           temp1 = cur.next;
           temp2 = cur.next.next;
           temp3 = cur.next.next.next;
           cur.next = temp2;
           temp2.next = temp1;
           temp1.next = temp3;
           cur = temp1;
       }
       return dummyhead.next;
        
    }
}

/**
 * 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 pre = head;
        ListNode cur = head.next;
        ListNode newNode = swapPairs(cur.next);//注意递归的顺序很重要
        pre.next = newNode;
        cur.next = pre;
        return cur;
    }
}

题目2:【二刷完成】

 

代码2:

/**
 * 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 fast = dummy;
        ListNode slow = dummy;
        for (int i=0; i<n+1; i++){
            fast = fast.next;
        }
        while(fast != null){ //这里不能是fast.next 
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
        
    }
}

题目3:【二刷完成】

代码3:

/**
 * 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;
        int size1=0;
        ListNode curB =  headB;
        int size2 = 0;
        int a,b;
        while(curA != null){
            size1++;
            curA = curA.next;
        }
        while(curB != null){
            size2++;
            curB = curB.next;
        }
        //这时别忘了cur指针还原到头节点
        curA = headA;
        curB = headB;
        //判断两个链表长短,然后进行对齐操作
        if (size1 > size2){
            a = size1-size2;
            for (int i=0;i<a;i++){
                curA = curA.next;
            }
        }else if (size1 < size2){
            b = size2-size1;
            for (int i=0;i<b;i++){
                curB = curB.next;
            }
        }
        /*
        while(curA != null){
            if (curA == curB){
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        */
        while(curA != curB){
            curA = curA.next;
            curB = curB.next;
        }
        if (curA == curB){
            return curA;
        }
        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) {
        ListNode curA = headA;
        ListNode curB = headB;
        int lenA=0;
        int lenB=0;
        while(curA!=null){
            curA = curA.next;
            lenA++;
        }
        while(curB!=null){
            curB = curB.next;
            lenB++;
        }
        curA = headA;
        curB = headB;
        if (lenA>lenB){
            for (int i=0;i<(lenA-lenB);i++){
                curA = curA.next;
            }
            while(curA!=curB){
                curA = curA.next;
                curB = curB.next;
            }
            return curA;
        }else{
            for (int i=0;i<(lenB-lenA);i++){
                curB = curB.next;
            }
            while(curA!=curB){
                curA = curA.next;
                curB = curB.next;
            }
            return curA;
        }
    }
}

题目4:【二刷完成】

代码4:

/**
 * 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;
        //fast / slow 指针开始移动
        //因为fast在前,所有需要先判断fast而不是slow
        //要判断fast指针指向的节点和没有指向的节点是否存在
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.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、付费专栏及课程。

余额充值