首页 > 其他分享 >代码随想录第四天 | 24. 两两交换链表中的节点 , 19.删除链表的倒数第N个节点, 面试题 02.07. 链表相交, 142.环形链表II

代码随想录第四天 | 24. 两两交换链表中的节点 , 19.删除链表的倒数第N个节点, 面试题 02.07. 链表相交, 142.环形链表II

时间:2022-10-15 15:00:44浏览次数:84  
标签:head ListNode temp 随想录 next 链表 null 节点

今天是第四天,今天的内容依旧延续昨天的课题,链表

 

24. 两两交换链表中的节点

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode res = new ListNode(-1);
        res.next = head;
        ListNode temp = head;
        ListNode pre = res;
        while (temp != null&&temp.next != null){
            
            ListNode cur = temp.next;
            ListNode post = temp.next.next;
            pre.next = cur;
            cur.next = temp;
            temp.next = post;
            post = temp;
            pre = temp;
            temp = temp.next;
        }
        return res.next;
    }
}


要用一个额外链表,储存需要换顺序的两个点之前的一个节点。

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

 

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null||head.next == null){
            return null;
        }
        ListNode a = head;
        ListNode b = head;
        for(int i = 0; i< n; i++){
            if(a.next != null){
                a = a.next;
            }
            else{
                return head.next;
            }
        }

        while(a.next!=null){
            a = a.next;
            b = b.next;
        }
        b.next = b.next.next;
        return head;
        
        
        
        

    }
}

双指针算法,让a指针先走过n个距离

面试题2.07 两表相交

/**
 * 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 a = headA;
        ListNode b = headB;

        while(a != b){
            if(a == null){
                a = headB;
            }
            else{
                a = a.next;
            }
            if(b == null){
                b = headA;
            }
            else
            {
                b = b.next;
            }
                
            }
            return a;
        }
        
    }

如果a与b有相同的,那么在a循环了bhead,和b循环了ahead之后,他们一定会于相同的点相遇。如果没有的话, 那么就会在null相遇。

142. 环形链表2

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode a = head;
        ListNode b = head;
        boolean isCycle = false;
        while(b.next != null && b.next.next != null){
            a = a.next;
            b = b.next.next;
            if(a==b){
                isCycle = true;
                break;
            }
            
        }
        if(isCycle){
            ListNode res = head;
            while(a != res){
                a = a.next;
                res = res.next;
            }
            return res;
        }
        else{
            return null;
        }
    }
}

第一步看快慢指针是否相遇,第二步让一个指针从head走,一个从环的起点走。他们相遇时的位置,就是环开始的位置

 

今天的题明显难起来了,需要的不光是单纯的数据结构的知识了

 

标签:head,ListNode,temp,随想录,next,链表,null,节点
From: https://www.cnblogs.com/catSoda/p/16794087.html

相关文章