算法day04 | Java | 链表 | LeetCode 24,19,面试题,142

55 阅读1分钟

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

忘记要用dummyhead了: 要想交换相邻两个节点,需要知道他俩前一个节点,所以要使用dummyhead

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head==null || head.next==null) return head;
        ListNode dummyHead = new ListNode(-1,head);
        ListNode cur = dummyHead;
        ListNode tmp1=null; ListNode tmp=null;

        while(cur.next != null && cur.next.next != null) { //这里不是或 是 与
            tmp = cur.next;
            tmp1 = cur.next.next;
            
            cur.next = tmp1;
            tmp.next = tmp1.next;
            tmp1.next =  tmp;

            cur = cur.next.next;
        }
        return dummyHead.next;
    }
}

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

写对了,这里要注意for循环的条件 for(int i=1; i<=n; i++) 是i<=n不是i<n

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyhead = new ListNode(-1,head);

        ListNode fast = dummyhead, slow=dummyhead;
        for(int i=1; i<=n; i++) {
          fast = fast.next;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummyhead.next;
    }
}

面试题 02.07. 链表相交

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode p1 = headA, p2 = headB;
        int lenA = 0, lenB = 0;
        while(p1 != null) {
            lenA++;
            p1=p1.next;
        }
        while(p2 != null) {
            lenB++;
            p2=p2.next;
        }

        p1 = headA;
        p2 = headB;
        //让p1指向长的那个
        if(lenB > lenA) {
            int len = lenA;
            lenA = lenB;
            lenB = len;

            ListNode tmp = p1;
            p1 = p2;
            p2 = tmp;
        }

        for(int i = 1; i <= lenA-lenB; i++ ) {
            p1 = p1.next;
        }

        while(p1 != null) {
            if(p1 == p2) {
                return p1;
            }
            p1 = p1.next;
            p2 = p2.next;
        }
        return null;
    }
}

分析这里为什么不可以写 p1.next,因为如果是下面这种写法的话,当相交的点在最后一个就无法判断出来。

image.png

142.环形链表II

我只记得用快慢指针可以判断有没有环。不记得怎么定位了。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null) return head;

        ListNode slow = head, fast = head;
        while(fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                //有环
                ListNode h = head;
                int count = 0;
                while(h != slow) {
                    h = h.next;
                    slow = slow.next;
                    count++;
                }
                return h;
            }
        }
        return null;
    }
}