链表合集1

最近在准备面试,把基础的算法稍微复习一下。

1. 链表反转        leeetcode 206 

        (1)最常用的,用两个相邻的指针依次向后遍历

    public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode prev = null;
        while(cur != null){
            ListNode nextNode = cur.next;
            cur.next = prev;
            prev = cur;
            cur = nextNode;
        }
        return prev;
    }

        (2) 用栈实现,记得放进去的时候每个节点next要设为Null,不然最后结果会有循环链表

    public ListNode reverseList(ListNode head) {
        Stack<ListNode> stack = new Stack<>();
        ListNode temp = null;
        while(head != null){
            stack.push(head);
            temp = head;
            head = head.next;
            temp.next = null;
        }
        ListNode newHead = new ListNode();
        ListNode cur = newHead;
        while(!stack.isEmpty()){
            cur.next = stack.pop();
            cur = cur.next;
        }
        return newHead.next;
    }

        (3) 头插法,需要生成一个新链表,额外空间复杂度O(N)

    public ListNode reverseList(ListNode head) {
        ListNode newHead = null;
        while(head != null){
            ListNode cur = new ListNode(head.val);
            cur.next = newHead;
            newHead = cur;
            head = head.next;
        }
        return newHead;
    }

        (4) 递归法,这个我想了一会才想明白。if里判断head == null是预防传进来就是个NULL,判断head.next == null是想返回最后一个节点。

    public ListNode reverseList(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode cur = reverseList(head.next);
        head.next.next = head;
        head.next = null;

        return cur;
    }

2. 找链表中点        leetcode 876

快慢指针,while里条件这么写的原因是:当链表长度为偶数时,fast先为NULL,当长度为奇数时,fast.next先为NULL,因此可以在while循环结束之后根据fast或fast.next哪个为NULL判断链表长度是奇数还是偶数。此外,在链表长度为偶数时,slow最后指向中间两个节点后一个,如果需要靠前一个中点,声明一个prev变量,每次slow往下移之前令prev = slow即可,一开始我想的是在找到slow之后再从头遍历一遍,太蠢了。

    public ListNode middleNode(ListNode head) {
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

3. 删除链表倒数第K个节点        leetcode 19

先说怎么找到倒数第K个节点:假设链表长度为n,先让一个指针fast走k步,然后slow指针从头开始和fast一直往后走,直到fast == null,这样fast又走了(n-k)步,加起来正好n步等于链表长度,slow也走了(n-k)步,此时slow距离链表结尾的null刚好k步,就等于倒数第k个节点。

要想删除的话,用一个prev指针,表示slow前一个节点,每次slow往后移之前让prev = slow。当slow到达倒数第k个节点后,需要删除slow节点,只需要让prev.next = prev.next.next即可,就跳过了slow节点。

特殊情况,当n=k,等于正好删除头节点,应当让新的头节点指向第二个节点(如果有的话)

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode slow = head, fast = head, prev = null;
        for(int i = 0; i < n; i++){
            fast = fast.next;
        }
        while(fast != null){
            prev = slow;
            slow = slow.next;
            fast = fast.next;
        }
        if(prev == null){
            return head.next;
        }
        prev.next = prev.next.next;
        return head;
    }

4. 两个升序的链表合并        leetcode 21

很简单,主要是这个方法在归并排序中有用,提醒自己复习一下排序

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode newHead = new ListNode();
        ListNode cur = newHead;
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val){
                cur.next = list1;
                list1 = list1.next;
            } else{
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        while(list1 != null){
            cur.next = list1;
            list1 = list1.next;
            cur = cur.next;
        }
        while(list2 != null){
            cur.next = list2;
            list2 = list2.next;
            cur = cur.next;
        }
        return newHead.next;
    }

5. 将原链表按照某个值分成左右两半,各自保留原来顺序        leetcode 86

这题以前第一次做的时候没看懂意思,注意不要使用额外空间就行,用两个小链表分别存大的和小的,最后首尾相连。

    public ListNode partition(ListNode head, int x) {
        ListNode big = new ListNode();
        ListNode small = new ListNode();
        ListNode curBig = big;
        ListNode curSmall = small;
        while(head != null){
            if(head.val >= x){
                curBig.next = new ListNode(head.val);
                curBig = curBig.next;
            } else{
                curSmall.next = new ListNode(head.val);
                curSmall = curSmall.next;
            }
            head = head.next;
        }
        curSmall.next = big.next;
        return small.next;
    }

6. 判断链表是不是回文结构        leetcode 234

这题以前第一次写的时候,居然存到了一个ArrayList里面然后首尾比较。现在写链表题注重不适用额外空间。因此找到中点,然后把后半部分链表反转,然后和前半部分链表挨个比较节点的值。

有趣的是,使用双指针法得到的反转链表,自然会和前半部分断开,然后如果原始链表长度为奇数,根据中点反转的后半部分链表是会包括原中点的,但是比较的时候不会有影响。最后,比较完之后可以把后半部分链表再恢复回去,再把前后连起来,需要多保存一个prev指向中点前一位。

class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head.next == null){
            return true;
        }
        ListNode[] temp = getMid(head);
        ListNode prev = temp[0], mid = temp[1];
        ListNode tail = reverseList(mid);
        boolean res = check(head, tail);
        ListNode oldMid = reverseList(tail);
        prev.next = oldMid;
        //print(head);
        return res;
    }
    private boolean check(ListNode root1, ListNode root2){
        while(root1 != null && root2 != null){
            if(root1.val != root2.val){
                return false;
            }
            root1 = root1.next;
            root2 = root2.next;
        }
        return true;
    }
    private ListNode[] getMid(ListNode root){
        ListNode slow = root, fast = root, prev = null;
        while(fast != null && fast.next != null){
            prev = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        return new ListNode[]{prev, slow};
    }
    private ListNode reverseList(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode cur = reverseList(head.next);
        head.next.next = head;
        head.next = null;

        return cur;
    }
    private void print(ListNode head){
        while(head != null){
            System.out.println(head.val + " ");
            head = head.next;
        }
    }
}

7. 判断两个链表是否相交        leetcode 160

如果有一个链表为空,肯定不相交;分别找到两个链表的最后一个非空节点,如果不相等肯定不相交;如果相交,在之前找最后节点的时候保存了两个链表的长度,长度差为p, 让长的那个头节点先走p步,然后两个头节点一起走,相等时就是相交节点。

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        
        int lengthA = 0, lengthB = 0;
        ListNode curA = headA, curB = headB;
        
        while(curA.next != null){
            curA = curA.next;
            lengthA++;
        }
        lengthA++;
        
        while(curB.next != null){
            curB = curB.next;
            lengthB++;
        }
        lengthB++;
        
        if(curA != curB){
            return null;
        }
        
        curA = headA;
        curB = headB;
        if(lengthA < lengthB){
            for(int i = 0; i < lengthB - lengthA; i++){
                curB = curB.next;
            }
        }
        if(lengthA > lengthB){
            for(int i = 0; i < lengthA - lengthB; i++){
                curA = curA.next;
            }
        }
        while(curA != null && curB != null){
            if(curA == curB){
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }

8. 链表里是否有环        leetcode 141

快慢指针,while里条件和找中点一样,就是保证不越界,如果原链表就一个节点,且next不指向自己,while循环根本进不了。一定要先移动slow和fast再判断是否相等!!!!

    public boolean hasCycle(ListNode head) {
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast){
                return true;
            }
        }
        return false;
    }

9. 如果链表有环,找到环开始的节点        leetcode 142

上一题的扩展。首先让快慢指针一直走直到相遇,如果没相遇直接退出了,说明没有环,返回空。

如果有环,设链表总长度为n,环的长度为k,则链表头到环第一个节点长度为(n-k)。当快慢指针相遇时,快指针比慢指针多走的就是环的长度,因此慢指针走了k步,此时让慢指针和head头指针一起往后移动,当它们相遇时,就刚好在环的第一个节点,它们也必然相遇在环的第一个节点。因为当慢指针走了k步和快指针相遇后,再走(n-k)步,头指针也走(n-k)步,此时慢指针刚好走完n步,所以和头指针相遇在环的第一个节点。

    public ListNode detectCycle(ListNode head) {
        ListNode slow = head, fast = head;
        boolean find = false;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast){
                find = true;
                break;
            }
        }
        if(!find){
            return null;
        }
        while(head != slow){
            head = head.next;
            slow = slow.next;
        }
        return slow;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值