当前位置: 首页 > 知识库问答 >
问题:

在LinkedList中搜索并将值放在Linkedlist的开头

鲁永福
2023-03-14

我的任务是搜索LinkedList中的值,如果找到某个值,则应将其删除并放置在LinkedList的开头。

class ListItem { 
    String value;
    ListItem next; 
    ListItem previous;
 
    ListItem(String value) { 
 this.value = value;
    } 
} 
 
 
class LinkedList { 
    ListItem first; 
    ListItem last; 
 
    public boolean contains(String v)  
    { 
 /* MODIFY THIS CODE */ 
 ListItem current = first; 
 
 while (current != null) 
 { 
 if (current.value.equals(v)) { 
 return true; 
 } 
 current = current.next; 
 } 
 return false; 
    } 
 
    public void add(String v){ 
         
 if (this.first == null) { 
 this.first = new ListItem(v); 
 } else { 
 ListItem i = this.first; 
 while (i.next != null){ 
 i = i.next;
 } 
 i.next = new ListItem(v); 
 } 
    } 
} 
 
LinkedList ll = new LinkedList(); 
ll.add("a");
ll.add("b");
ll.add("c");
 

assert ll.contains("c"); 
assert ll.first.value.equals("c"); 
assert ll.contains("d") == false; 
assert ll.first.value.equals("c"); 

共有1个答案

蒋向笛
2023-03-14

如果能有所帮助的话。

class Node<T>{
    T data;
    Node<T> next;

    Node(T data){
        this.data = data;
    }
}

class SearchInsert{

    public static Node<Integer> createLinkedList(){
        Node<Integer> n1 = new Node<>(10);
        Node<Integer> n2 = new Node<>(20);
        Node<Integer> n3 = new Node<>(30);
        Node<Integer> n4 = new Node<>(40);
        Node<Integer> n5 = new Node<>(50);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        return n1;
    }

    public static Node<Integer> searchAndInsertAtBegining(Node<Integer> head, int value){ // value is the data for which we search in the list and insert it at the begining
        int pos = 0;
        int found = 0;
        Node<Integer> tempHead = head;
        Node<Integer> prev = head;
        while(head != null){
            if(head.data == value){
                found++;
                break;
            }
            pos++;
            head = head.next;
        }

        if (found == 1) {
            if (pos == 0) {
                return tempHead;
            }
            else{
                Node<Integer> nodeToBeDeleted = head; //node which are inserted at the begining of the list was found in the above whoile loop.
                int count = 0;
                while(count < pos -1 && prev.next != null){ 
                    count++;
                    prev = prev.next;
                }
                if (prev.next != null) {
                    prev.next = nodeToBeDeleted.next;
                    nodeToBeDeleted.next  = tempHead;
                    return nodeToBeDeleted;
                }
            }
        }
        return null;
    }

    public static void printlist(Node<Integer> head){

            //print recursively
        if (head == null) {
            return;
        }
        System.out.print(head.data + " ");
        printlist(head.next);
    }

    public static void main(String[] args) {
        Node<Integer> output = searchAndInsertAtBegining(createLinkedList(), 50);
        printlist(output);
    }

}
 类似资料:
  • 这里的问题是什么?。我正在尝试实现图数据结构,使用邻接列表,通过使用来自util包的集合。这里 包含一些整数的LinkedList数组。LinkedList的每个元素都包含另一个类型为:node的LinkedList。 但在编译过程中,它表示不兼容类型。如何解决这个问题?

  • 我试图解决以下问题algo专家级: 编写一个函数,该函数接收一个单向链接列表的头部和一个整数,按位置移动列表(即,不创建一个全新的列表),并返回其新头部。 移动链接列表意味着向前或向后移动其节点,并在适当的情况下将其环绕在列表中。例如,将链表向前移动一个位置将使其尾部成为链表的新头部。 节点向前还是向后移动取决于是正还是负。 每个节点都有一个整数,还有一个节点,指向列表中的下一个节点,或者如果是列

  • 问题内容: 我正在尝试使用LinkedList属性持久化一个类,但似乎无法正确处理。这是我的代码和映射: hibernate映射: 看来我可以像这样持久化Class Stuff的对象,但是当我尝试恢复它们时,发生以下错误: 问题答案: 通常,Hibernate将为集合提供其自己的实现,因此您应首选接口而非特定的实现。它可能正在尝试为图像分配其他类型的列表,但失败了。您必须将字段更改为。

  • LinkedList类扩展了AbstractSequentialList并实现了List接口。 它提供了一个链表数据结构。 以下是LinkedList类支持的构造函数。 Sr.No. 构造函数和描述 1 LinkedList( ) 此构造函数构建一个空链表。 2 LinkedList(Collection c) 此构造函数构建一个链接列表,该列表使用集合c的元素进行初始化。 除了从其父类继承的方法

  • 公共类插入节点{ } 您好,代码在LinkedList add head和add last的实现之上。但是,当我运行代码时,我可以添加新节点作为链表上的最后一个节点,但我不能将新节点添加到链表的请求中。 运行此代码时,输出为: 加数法有效,但为什么不加前置呢?

  • 我需要一个线程安全的并发列表,同时最适合迭代,并且应该返回精确的大小。我想存储物品的拍卖出价。所以我想能够 检索项目的确切出价数量 为项目添加出价 检索给定项目的所有出价。 移除商品出价 我打算把它放在