【编程】链表问题

本文介绍两种链表操作:一是利用头插法创建链表并实现链表的翻转;二是介绍两种方法删除链表中重复的节点,一种使用HashSet辅助,时间与空间复杂度均为O(n),另一种仅用链表本身的空间,空间复杂度为O(1),但时间复杂度为O(n²)。

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

1。翻转单向链表
2。删除无序链表中值重复的节点

import java.util.HashSet;
import java.util.Scanner;
public class Node {
    public int value;
    public Node next;
    public Node(int data) {
        this.value = data;
        next = null;
    }
    public Node() {
        next = null;
    }
    //翻转链表
    public static Node reverseList(Node head) {
        Node pre = null;
        Node next = null;
        while(head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
    /**
     * 头插法创建链表,手动输入
     */
    public static Node createLinkedList() {
        Node head = null;
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()) {
            int reader = scanner.nextInt();
            Node newNode = new Node(reader);

            newNode.next = head;
            head = newNode;
        }
        return head;
    }
    /*
     * 遍历元素
     */
    public static void travelLinkedList(Node head) {
        Node pnext = head;
        while(pnext != null) {
            System.out.print(pnext.value + "  ");
            pnext = pnext.next;
        }
        System.out.println();
    }
    /*
     * 删除无序链表中值重复的节点, 借助HashSet空间复杂度O(n),时间复杂度O(n)
     */
    public static void removeRepeatElementMethodOne(Node head) {
        if(head == null) return;
        else {
            HashSet<Integer> set = new HashSet<Integer>();
            Node pre = head;
            Node curr = head.next;
            set.add(head.value);
            while(curr != null) {
                if(set.contains(curr.value)) {
                    pre.next = curr.next;
                } else {
                    set.add(curr.value);
                    pre = curr;
                }
                curr = curr.next;
            }
        }
    }
    /*
     * 除无序链表中值重复的节点,空间复杂度O(1)时间复杂度O(n^2)
     */
    public static void removeRepeatElementMethodTwo(Node head) {
        Node cur = head;
        Node pre = null;
        Node next = null;
        while(cur != null) {
            pre = cur;
            next = cur.next;
            while(next != null) {
                if(cur.value == next.value) {
                    pre.next = next.next;
                }else {
                    pre = next;
                }
                next = next.next;
            }
            cur = cur.next;
        }
    }
    //测试翻转单向链表
    public static void testReverseList() {
        Node head = createLinkedList();
        //员链表顺序
        travelLinkedList(head);
        Node newHead = reverseList(head);
        //翻转后链表顺序
        travelLinkedList(newHead);
    }
    //测试删除无序链表中值重复的节点
    public static void testRemoveRepeatElementOne() {
        Node head = createLinkedList();
        removeRepeatElementMethodTwo(head);
        //removeRepeatElementMethodOne(head);
        travelLinkedList(head);
    }
    public static void main(String[] args) {
        //testReverseList();
        testRemoveRepeatElementOne();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值