zoukankan      html  css  js  c++  java
  • 算法 删除单链表中重复元素

    Write code to remove duplicates from an unsorted linked list.
    FOLLOW UP
    How would you solve this problem if a temporary buffer is not allowed?

    package com;
    
    public class Test {
    
    	private static class LinkedListNode {
    		private char c;
    		private LinkedListNode next;
    		LinkedListNode(char c){
    			this.c = c;
    		}
    		public String toString(){
    			return c + (next != null ? next.toString() : "");
    		}
    	}
    
    	public static void deletdDup2(LinkedListNode head) {
    		LinkedListNode i = head.next;
    		LinkedListNode p = head;
    		LinkedListNode j;
    		for (; i != null; i = i.next) {
    			for (j = head;j != i; j = j.next) {
    				if (i.c == j.c) {
    					p.next = i.next;
    					break;
    				}
    			}
    			if(i == j){
    				p = p.next;
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		LinkedListNode one = new LinkedListNode('a');
    		LinkedListNode two = new LinkedListNode('b');
    		LinkedListNode three = new LinkedListNode('c');
    		LinkedListNode four = new LinkedListNode('a');
    		LinkedListNode five = new LinkedListNode('c');
    		LinkedListNode six = new LinkedListNode('d');
    		LinkedListNode seven = new LinkedListNode('b');
    		LinkedListNode eight = new LinkedListNode('a');
    		one.next = two;
    		two.next = three;
    		three.next = four;
    		four.next = five;
    		five.next = six;
    		six.next = seven;
    		seven.next = eight;
    		eight.next = null;
    		System.out.println(one);
    		deletdDup2(one);
    		System.out.println(one);
    	}
    
    }
    

      

  • 相关阅读:
    后缀数组/LCP
    Manachar's Algorithm
    自动AC机qwq(大雾)以及trie图fail图的一些结论
    平衡树有关题目小结
    洛谷日报索引(转)
    初始化函数的简单写法
    关于对拍
    第12组 Alpha冲刺(4/6)
    第12组 Alpha冲刺(3/6)
    第12组 Alpha冲刺(2/6)
  • 原文地址:https://www.cnblogs.com/waxili/p/2976798.html
Copyright © 2011-2022 走看看