[劍指offer] JAVA版題解 面試題18

在這裏插入圖片描述

代碼演示

package swordfingeroffer;

/**
 * create by
 *
 * @Author 羅志遠
 * @on 2020-05-27.
 * @time 23:27
 */
public class InterviewQuestion18 {
    public static class ListNode{

        int value;
        ListNode next;

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

    public ListNode deleteListNode(ListNode listHeader,ListNode deleteNode){

        if (listHeader == null || deleteNode == null){
            return null;
        }
        // 要刪除的節點在中間
        if (deleteNode.next != null){
            ListNode tempListNode = deleteNode.next;
            deleteNode.value = tempListNode.value;
            deleteNode.next = tempListNode.next;
        }else if (listHeader == deleteNode){
            listHeader = null;
        }else {
            ListNode tempListNode = listHeader;
            while (tempListNode.next != deleteNode){
                tempListNode = tempListNode.next;
            }
            tempListNode.next = null;
        }

        return listHeader;
    }

    public void printList(ListNode listNode) throws Exception {

        if (listNode == null){
            throw new Exception("鏈表不能爲空!");
        }

        while (listNode != null){
            System.out.print(listNode.value + " ");
            listNode = listNode.next;
        }
    }

    public static void main(String[] args) {
        InterviewQuestion18 interviewQuestion18 = new InterviewQuestion18();
        ListNode listHeader = new ListNode(5);
        ListNode listNode1 = new ListNode(9);
        ListNode listNode2 = new ListNode(10);
        ListNode listNode3 = new ListNode(15);

        listHeader.next = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listHeader = interviewQuestion18.deleteListNode(listHeader, listHeader);
        try {
            interviewQuestion18.printList(listHeader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在這裏插入圖片描述

代碼演示:

package swordfingeroffer;

/**
 * create by
 *
 * @Author 羅志遠
 * @on 2020-05-29.
 * @time 21:57
 */
public class InterviewQuestion182 {
    public static class ListNode{
        int value;
        ListNode next;

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

    public ListNode deleteDuplication(ListNode pHead) {
        if (pHead == null) {
            throw new RuntimeException("鏈表不能爲空!");
        }
        ListNode preNode = null;
        ListNode pNode = pHead;
        while (pNode != null) {
            ListNode pNext = pNode.next;
            boolean toDelete = false;
            if (pNext != null && pNext.value == pNode.value) {
                toDelete = true;
            }
            if (!toDelete) {
                preNode = pNode;
                pNode = pNext;
            } else {
                ListNode pToBeDelete = pNode;
                int value = pNode.value;
                while (pToBeDelete != null && pToBeDelete.value == value) {
                    pNext = pToBeDelete.next;
                    pToBeDelete = pNext;
                }
                if (preNode == null) {
                    pHead = pNext;
                } else {
                    preNode.next = pNext;
                }
                pNode = pNext;
            }
        }
        return pHead;
    }

    public void printList(ListNode pHead) {
        if (pHead == null) {
            throw new RuntimeException("打印鏈表不能爲空!");
        }
        ListNode pNode = pHead;
        while (pNode != null) {
            System.out.print(pNode.value + " ");
            pNode = pNode.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        InterviewQuestion182 interviewQuestion182 = new InterviewQuestion182();
        ListNode pHead = new ListNode(5);
        ListNode listNode1 = new ListNode(6);
        ListNode listNode2 = new ListNode(6);
        ListNode listNode3 = new ListNode(7);

        pHead.next = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        pHead = interviewQuestion182.deleteDuplication(pHead);
        interviewQuestion182.printList(pHead);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章