链表相关面试题

public class Code01_LinkedListMid {
    public static void main(String[] args) {

    }

    public static class Node{
        public int value;
        public Node next;

        public Node(int v){
            this.value = v;
        }
    }

    // 1 2 3 4 5  返回中点或上中点
    public static Node midOrUpMidNode(Node head){
        if(null == head || head.next == null || head.next.next==null){
            return head;
        }

        // 链表有3个点或以上
        Node slow = head.next;
        Node fast = head.next.next;
        while(null != fast.next && null != fast.next.next){
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }

    // 1 2 3 4 5  返回中点或下中点
    public static Node midOrDownMidNode(Node head){
        if(null == head || head.next == null){
            return head;
        }

        Node slow = head.next;
        Node fast = head.next;
        while(null != fast.next && null != fast.next.next){
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }

    // (中点或上中点)的前一个 1 2 3 4 5
    public static Node midOrUpMidPreNode(Node head){
        if(null == head || head.next == null || head.next.next==null){
            return null;
        }

        Node slow = head;
        Node fast = head.next.next;
        while(null != fast.next && null != fast.next.next){
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }

    // (中点或下中点)的前一个 1 2 3 4 5
    public static Node midOrDownMidPreNode(Node head){
        if(null == head || head.next == null){
            return null;
        }

        if(head.next.next == null){
            return head;
        }

        Node slow = head;
        Node fast = head.next;
        while(null != fast.next && null != fast.next.next){
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }
}

 

判断一个链表是否是回文结构

public class IsPalindromeTest {
    public static void main(String[] args) {
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(2);
        Node n5 = new Node(1);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;

        System.out.println(isPalindrome(n1));
    }

    public static class Node{
        private int value;
        private Node next;
        public Node(int value){
            this.value = value;
        }
    }

    public static boolean isPalindrome(Node head){
        if(null == head || head.next == null){
            return true;
        }

        Node n1 = head;
        Node n2 = head;

        while(n2.next != null && n2.next.next != null){
            n1 = n1.next;
            n2 = n2.next.next;
        }

        n2 = n1.next;
        n1.next = null;
        Node n3 = null;
        // 逆序链表
        while(n2 != null){
            n3 = n2.next; // save next node
            n2.next = n1; // next of right node convert
            n1 = n2; // n1 move
            n2 = n3; // n2 move
        }

        n3 = n1; // save last node 最后一个节点
        n2 = head; // 左边第一个节点
        boolean res = true;
        while(n2 != null && n1 != null){
            if(n2.value != n1.value){
                res = false;
                break;
            }
            n1 = n1.next;
            n2 = n2.next;
        }

        // 将逆序的链表再还原回来
        n1 = n3.next;
        n3.next = null;
        while(n1 != null){
            n2 = n1.next;
            n1.next = n3;
            n3 = n1;
            n1 = n2;
        }

        return res;
    }
}

 

给出一个链表,小于某个数的放在链表左边,等于某个数的放在链表中间,大于某个数的放在链表右边

public class SmallEqualsBigger {
    public static void main(String[] args) {
        Node n1 = new Node(5);
        Node n2 = new Node(3);
        Node n3 = new Node(3);
        Node n4 = new Node(2);
        Node n5 = new Node(1);
        Node n6 = new Node(6);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = n6;

        n1 = listPartition2(n1,3);
        while(n1 != null){
            System.out.print(n1.value+ "  ");
            n1 = n1.next;
        }
    }

    public static class Node{
        private int value;
        private Node next;
        public Node(int value){
            this.value = value;
        }
    }

    // 方法一
    public static Node listPartition1(Node head, int pivot){
        if(head == null){
            return head;
        }

        Node cur = head;
        int i = 0;
        while(cur != null){
            i++;  // 计算出链表节点的总个数
            cur = cur.next;
        }

        Node[] nodeArr = new Node[i];
        i = 0;
        cur = head;
        // 把链表放到数组中
        for (i = 0; i < nodeArr.length; i++) {
            nodeArr[i] = cur;
            cur = cur.next;
        }

        arrPartition(nodeArr, pivot);
        for (i = 1; i != nodeArr.length; i++) {
            nodeArr[i-1].next = nodeArr[i];
        }

        nodeArr[i-1].next = null; // 最后一个节点
        return nodeArr[0];// 返回新的头节点
    }

    public static void arrPartition(Node[] nodeArr, int pivot){
        int small = -1;
        int big = nodeArr.length;
        int index = 0;
        while(index<big){
            if(nodeArr[index].value < pivot){
                swap(nodeArr,index++,++small);
            }else if(nodeArr[index].value == pivot){
                index++;
            }else if(nodeArr[index].value > pivot){
                swap(nodeArr,index,--big);
            }
        }
    }

    public static void swap(Node[] nodeArr, int node1, int node2){
        Node temp = nodeArr[node1];
        nodeArr[node1] = nodeArr[node2];
        nodeArr[node2] = temp;
    }

    // 方法二
    public static Node listPartition2(Node head, int pivot){
        // 使用6个变量
        Node sH = null;  // small head
        Node sT = null;  // small tail
        Node eH = null;  // equal head
        Node eT = null;  // equal tail
        Node bH = null;  // big head
        Node bT = null;  // big tail
        Node next = null; // save next node

        while(head != null){
            next = head.next;
            head.next = null;

            if(head.value < pivot){
                if(sH == null){
                    sH = head;
                    sT = head;
                }else{
                    sT.next = head;
                    sT = head;
                }
            }else if(head.value == pivot){
                if(eH == null){
                    eH = head;
                    eT = head;
                }else{
                    eT.next = head;
                    eT = head;
                }
            }else {
                if(bH == null){
                    bH = head;
                    bT = head;
                }else{
                    bT.next = head;
                    bT = head;
                }
            }

            head = next;
        }

        if(sT != null){
            sT.next = eH;
            eT = eT == null ? sT : eT; // 下一步,谁去连大于区域的头,谁就变成eT
        }

        if(eT != null){
            eT.next = bH;
        }

        sH = sH == null ? (eH == null ? bH: eH): sH;
        return sH;
    }
}

 

public class Code02_CopyListWithRand {
    public static void main(String[] args) {
        Node n1 = new Node(5);
        Node n2 = new Node(4);
        Node n3 = new Node(3);
        Node n4 = new Node(2);
        Node n5 = new Node(1);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;

        Node newN1 = copyListWithRand2(n1);
        while(null != newN1){
            System.out.print(newN1.value+" ");
            newN1 = newN1.next;
        }
    }

    public static class Node{
        public int value;
        public Node next;
        public Node random;

        public Node(int value){
            this.value = value;
        }
    }

    public static Node copyListWithRand2(Node head){
        if(null == head){
            return null;
        }

        Node cur = head;
        Node next = null;

        // 1 -> 2
        // 1 -> 1' -> 2
        while(null != cur){
            next = cur.next;
            // 新生成的节点,插入到原来节点后面
            cur.next = new Node(cur.value);
            cur.next.next = next;
            cur = next;
        }

        cur = head;
        Node curCopy = null;
        // 1 -> 1' -> 2 -> 2'
        while(null != cur){
            next = cur.next.next;
            curCopy = cur.next;
            curCopy.random = (cur.random == null ? null : cur.random.next);
            cur = next;
        }

        Node res = head.next;
        cur = head;
        while(cur != null){
            next = cur.next.next;
            curCopy = cur.next;
            cur.next = next;
            curCopy.next = (null != next ? next.next : null);
            cur = next;
//            cur.next = cur.next.next;
//            cur = cur.next;
        }

        return res;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值