[Offer] [18-2] [delete duplicate node list]

Title Description

  In a sorted linked list nodes duplicate, delete the duplicate node list, the node does not retain repeated, returns the head pointer list. For example, the list 1-> 2-> 3-> 3-> 4-> 4-> 5 is treated 1-> 2-> 5

Ideas analysis

  Because it is an ordered list, to delete duplicate nodes,, we can set the three pointers, pre, p, next, point to the previous node, the current node and the next node, traverse the list, determining the current node and the next node are the same, a flag bit is provided needDel:
  1. If none is found, the pointer is moved backward wanted;
  2. otherwise, it is set true, then you can continue searching for next cycle through repeating node (since lists are ordered )
Note:

  1. The list with only one node
  2. Deleted is the case of the first node

Test Case

  1. Function Test (duplicate nodes located in the head of the list, the central tail, no duplicate nodes)
  2. Special tests (null, all nodes are repeated)

Java code

public class Offer18_02 {

    public static void main(String[] args) {
        System.out.println("****功能测试***");
        test1();
        System.out.println("***特殊输入测试****");
        test2();
    }

    public static ListNode deleteDuplication(ListNode pHead) {
        return Solution1(pHead);
    }

    /**
     * 
     * 需要设置三个指针,pre pNode 和 next ;
     * 
     * @param pHead
     * @return
     */
    private static ListNode Solution1(ListNode pHead) {
        if (pHead == null || pHead.next == null) {// 如果链表为空或者只有一个节点
            return pHead;
        }
        ListNode pre = null;
        ListNode pNode = pHead; // 代表当前节点
        while (pNode != null) {// 从头结点开始遍历链表
            boolean needDel = false;
            ListNode pNext = pNode.next;
            if (pNext != null && pNext.val == pNode.val) {
                // 如果当前节点的下一个结点不为null 且其值和当前节点相等
                needDel = true;
            }
            if (!needDel) {// 如果没有找到重复的元素就将指针往后移动
                pre = pNode;
                pNode = pNode.next;
            } else { // 找到重复的元素
                ListNode toBeDel = pNode;
                int value = toBeDel.val;
                while (toBeDel != null && toBeDel.val == value) {
                    // 将指针移到所有重复元素的后面
                    toBeDel = toBeDel.next;
                }
                if (pre == null) {// 删除的是第一个节点
                    pHead = toBeDel;
                } else {
                    pre.next = toBeDel;
                }
                pNode = toBeDel;
            }
        }

        return pHead;
    }

    /**
     * 功能测试
     */
    private static void test1() {//
        System.out.println("有两对重复的");
        ListNode node6 = new ListNode(6, null);
        ListNode node5 = new ListNode(6, node6);
        ListNode node4 = new ListNode(3, node5);
        ListNode node3 = new ListNode(3, node4);
        ListNode node2 = new ListNode(2, node3);
        ListNode node1 = new ListNode(1, node2);
        ListNode pHead = new ListNode(-1, node1);// 头结点
        System.out.println("删除之前");
        printListNode(pHead);
        System.out.println("删除之后--->");
        deleteDuplication(pHead);
        printListNode(pHead);
    }

    /**
     * 
     */
    private static void test2() {//
        System.out.println("所有的元素都是重复的");
        ListNode node6 = new ListNode(1, null);
        ListNode node5 = new ListNode(1, node6);
        ListNode node4 = new ListNode(1, node5);
        ListNode node3 = new ListNode(1, node4);
        ListNode node2 = new ListNode(1, node3);
        ListNode node1 = new ListNode(1, node2);
        ListNode pHead = new ListNode(-1, node1);// 头结点
        System.out.println("删除之前");
        printListNode(pHead);
        System.out.println("删除之后--->");
        deleteDuplication(pHead);
        printListNode(pHead);
    }

    /**
     * 打印链表
     * 
     * @param pHead
     */
    private static void printListNode(ListNode pHead) {
        ListNode p = pHead.next;
        while (p != null) {
            System.out.print(p.val + " ");
            p = p.next;
        }
        System.out.println();
    }

}

Code link

Offer to prove safety codes -Java

Guess you like

Origin www.cnblogs.com/haoworld/p/offer182-shan-chu-lian-biao-zhong-zhong-fu-de-jie-.html