刷题:链表相关2

DAY 4

24 两两交换链表中的结点

题目要求:
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

输入:head = [1,2,3,4]
输出:[2,1,4,3]

代码:

/**
 * 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;  
        
        // 用cur指针指向dummyhead是因为我们要修改例如【1-->2】必须知道前一个结点指针才能改变


        // 得保证前两个元素不空
        while(cur.next != null && cur.next.next != null){

            //先保存指针: 再去做赋值操作,因为做了赋值操作之后指针就断掉了 
            ListNode  temp1 = cur.next;
            ListNode  temp2 = cur.next.next.next;
            
            //做赋值操作
            cur.next = temp1.next;
            cur.next.next = temp1;
            temp1.next = temp2;
            cur = cur.next.next;
          
        }
        return dummyHead.next;
    
    }
}

时间复杂度:O(n)
空间复杂度:O(1)

注意事项:
注意保存结点!!先保存结点再去做赋值操作!!防止链断开
使用虚拟头结点,方便头结点的操作!

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

题目要求:
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]

代码:自己提交的版本

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

        int size = 0;// 先初始化链表的长度为

        ListNode dummyHead = new ListNode(0,head);
        dummyHead.next = head;
        ListNode cur = dummyHead.next;

        // 先计算链表的长度
        while(cur != null){

            size++;
            cur = cur.next;
           
        }
       
       
        ListNode cur2 = dummyHead;
        //删除倒数第n个---》 要删除的元素就是正数第  size-1+n 个元素
        for(int i = 0; i < size - n ;i++){
            // 遍历三次就够了
            cur2 = cur2.next;
        } 
        cur2.next = cur2.next.next;
        return dummyHead.next;

    }
}

另外一种思路使用快慢指针做:

//明天在补充:

02.07 链表相交

题目要求:
给你两个单链表的头节点headAheadB,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at ‘8’
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

思路1 :
我们求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置,如
思路:curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到curA == curB,则找到交点。
代码:自己的

/**
 * 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, lenB = 0;
        while (curA != null) { // 求链表A的长度
            lenA++;
            curA = curA.next;
        }
        while (curB != null) { // 求链表B的长度
            lenB++;
            curB = curB.next;
        }

        curA = headA;
        curB = headB;

        int gap = Math.abs(lenA-lenB);
        
        if(lenA  == lenB){
            while( curA != null && curB != null){
                if(curA == curB){
                    return curA;
                }
                curA = curA.next;
                curB = curB.next;
            }
         
        }

        if(lenB > lenA){
            for(int i = 0; i < gap; i++){
                curB = curB.next;
            }
            while( curA != null ){
                if(curA == curB){
                    return curA;
                }
                curA = curA.next;
                curB = curB.next;
            }
        }


        if(lenA > lenB){
            for(int i = 0; i < gap; i++){
                curA = curA.next;
            }
            while( curB != null){
                if(curA == curB){
                    return curB;
                }
                curA = curA.next;
                curB = curB.next;
            }
        }
        return null;
    }     
}

改进的:

//明天在修改

142 环形链表

题目要求:
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

不允许修改 链表

解题思路:

用快慢指针判断是否有环
定义一个快指针 每次走两个结点
慢指针 每次走1个结点
环的入口:
等式的连接桥梁:

/**
 * 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) {// 有环
                ListNode index1 = fast;
                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、付费专栏及课程。

余额充值