【编程题】删除单链表重复元素(Java实现)(Shopee笔试题)

本文介绍了一种使用Java实现的单链表去重算法,该算法通过HashMap记录节点值,有效删除链表中重复的元素,同时保持原有顺序。适用于Shopee等公司的编程笔试题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【编程题】删除单链表重复元素(Java实现)(Shopee笔试题)

题目来源

(Shopee笔试题)

题目描述

对一个单链表去重,如果链表中有重复的项则,删除第二项以及以后的项,需要对链表保持顺序。
输入描述:
输入:
带有重复项的单向链表
输出描述:
输出:
不含有重复项的单向链表
例如:
输入
1 2 3 7 2 1 3 3 9
输出
1 2 5 7 3 9

解答

方法一 输入元素固定时

import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class Main1{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        ListNode listNode=new ListNode(0);
        ListNode head=listNode;
        //int n=sc.nextInt();
        for(int i=0;i<9;i++){//输入元素为9个时
            head.next=new ListNode(sc.nextInt());
            head=head.next;
        }
        deleteNode(listNode.next);
    }
    public static void deleteNode(ListNode head){
        if(head==null||head.next==null)return ;
        ListNode printNode=new ListNode(-1);
        printNode.next=head;
        ListNode pre =printNode;
        ListNode currentNode= printNode.next;
        HashMap<Integer,Integer>map=new HashMap<>();
        while(pre!=null&&pre.next!=null){
            if(map.containsKey(currentNode.val)){
                pre.next=currentNode.next;
                currentNode=currentNode.next;
            }
            else{
               map.put(currentNode.val,1);
               pre=currentNode;
               currentNode=currentNode.next;
            }

        }
        printNode=printNode.next;
        while (printNode!=null){

            System.out.println(printNode.val);
            printNode=printNode.next;

        }
    }
}
class ListNode{
    int val;
    ListNode next;
    ListNode(int val){
        this.val=val;
    }
}

方法一 输入元素不确定时

import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class Main4{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        ListNode listNode=new ListNode(0);
        ListNode head=listNode;
        //int n=sc.nextInt();
        String[] numArrays=sc.nextLine().split(" ");
        for(int i=0;i<numArrays.length;i++){
            head.next=new ListNode(Integer.valueOf(numArrays[i]));
            head=head.next;
        }
        deleteNode(listNode.next);

    }
    public static void deleteNode(ListNode head){
        if(head==null||head.next==null)return ;
        ListNode printNode=new ListNode(-1);
        printNode.next=head;
        ListNode pre =printNode;
        ListNode currentNode= printNode.next;
        HashMap<Integer,Integer>map=new HashMap<>();
        while(pre!=null&&pre.next!=null){
            if(map.containsKey(currentNode.val)){
                pre.next=currentNode.next;
                currentNode=currentNode.next;
            }
            else{
                map.put(currentNode.val,1);
                pre=currentNode;
                currentNode=currentNode.next;
            }

        }
        printNode=printNode.next;
        while (printNode!=null){

            System.out.println(printNode.val);
            printNode=printNode.next;

        }
    }
}
class ListNode{
    int val;
    ListNode next;
    ListNode(int val){
        this.val=val;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值