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);
    	}
    
    }
    

      

  • 相关阅读:
    DP入门——迷宫行走方案2
    DP入门——迷宫行走方案1
    DFS入门——八皇后问题输出方案
    matplotlib的学习3-figure图像
    matplotlib的学习2-基本用法
    matplotlib的学习1-为什么学他
    numpy的好处
    pandas的学习8-pandas-plot出图
    pandas的学习6-合并concat
    pandas的学习5-导入导出数据
  • 原文地址:https://www.cnblogs.com/waxili/p/2976798.html
Copyright © 2011-2022 走看看