zoukankan      html  css  js  c++  java
  • leetcode 面试题 02.01. 移除重复节点

    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * @Class RemoveDuplicateNodes
     * @Description 面试题 02.01. 移除重复节点
     * 编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。
     * <p>
     * 示例1:
     * 输入:[1, 2, 3, 3, 2, 1]
     * 输出:[1, 2, 3]
     * <p>
     * 示例2:
     * 输入:[1, 1, 1, 1, 2]
     * 输出:[1, 2]
     * <p>
     * 提示:
     * 链表长度在[0, 20000]范围内。
     * 链表元素在[0, 20000]范围内。
     * <p>
     * @Author 
     * @Date 2020/6/26
     **/
    public class RemoveDuplicateNodes {
        static class ListNode {
            int val;
            ListNode next;
    
            ListNode(int x) {
                val = x;
            }
        }
    }
    
    /**
     * 解法: 利用set不重复的性质或者哈希表,额,HashSet底层就是HashMap
     */
    public static ListNode removeDuplicateNodes(ListNode head) {
    	if (head == null) {
    		return null;
    	}
    
    	ListNode newListHead = new ListNode(0);
    	ListNode newListNode = newListHead;
    	Set<Integer> integerSet = new HashSet<>();
    	ListNode listNode = head;
    
    	while (listNode != null) {
    		int val = listNode.val;
    		if (integerSet.add(val)) {
    			ListNode tempNode = new ListNode(val);
    			newListNode.next = tempNode;
    			newListNode = newListNode.next;
    		}
    		listNode = listNode.next;
    	}
    	return newListHead.next;
    }
    
    // 测试用例
    public static void main(String[] args) {
    	// 1, 2, 3, 3, 2, 1
    	ListNode listNode11 = new ListNode(1);
    	ListNode listNode12 = new ListNode(2);
    	listNode11.next = listNode12;
    	ListNode listNode13 = new ListNode(3);
    	listNode12.next = listNode13;
    	ListNode listNode14 = new ListNode(3);
    	listNode13.next = listNode14;
    	ListNode listNode15 = new ListNode(2);
    	listNode14.next = listNode15;
    	ListNode listNode16 = new ListNode(1);
    	listNode15.next = listNode16;
    	listNode16.next = null;
    	ListNode listNodeAns = removeDuplicateNodes(listNode11);
    	System.out.print("demo01 result:");
    	while (listNodeAns != null) {
    		System.out.print(" " + listNodeAns.val);
    		listNodeAns = listNodeAns.next;
    	}
    	System.out.println("");
    
    	//[1, 1, 1, 1, 2]
    	ListNode listNode21 = new ListNode(1);
    	ListNode listNode22 = new ListNode(1);
    	listNode21.next = listNode22;
    	ListNode listNode23 = new ListNode(1);
    	listNode22.next = listNode23;
    	ListNode listNode24 = new ListNode(1);
    	listNode23.next = listNode24;
    	ListNode listNode25 = new ListNode(2);
    	listNode24.next = listNode25;
    	listNodeAns = removeDuplicateNodes(listNode21);
    	System.out.print("demo02 result:");
    	while (listNodeAns != null) {
    		System.out.print(" " + listNodeAns.val);
    		listNodeAns = listNodeAns.next;
    	}
    	System.out.println("");
    }
    
  • 相关阅读:
    MVC 下载相关
    中缀、前缀和后缀表达式
    什么是堆栈?
    为什么和其他语言相比C语言是快速的语言
    什么是回溯法?
    产生n bit所有可能的序列
    讨论汉诺塔之谜
    递归和内存分配(可视化)
    关于递归
    随机数产生函数的范围转换
  • 原文地址:https://www.cnblogs.com/fyusac/p/13194074.html
Copyright © 2011-2022 走看看