首页 \ 问答 \ LinkedList findElement(LinkedList findElement)

LinkedList findElement(LinkedList findElement)

我尝试使用下面的代码来查找LinkedList最后一个第k个元素。 为什么它总是返回null

public Node findElem(Node head, int k){
    if(k < 1|| k > this.length()){
        System.out.println("error");
    }
    Node p1=head;
    Node p2 = head;
    for(int i=0;i<k-1;i++) {
        p1 = p1.next;
    }
    while(p1 != null){
            p1= p1.next;
            p2 = p2.next;
    }
    return p2;
}

I try to use below code to find the last k-th element in the LinkedList. Why does it always return null?

public Node findElem(Node head, int k){
    if(k < 1|| k > this.length()){
        System.out.println("error");
    }
    Node p1=head;
    Node p2 = head;
    for(int i=0;i<k-1;i++) {
        p1 = p1.next;
    }
    while(p1 != null){
            p1= p1.next;
            p2 = p2.next;
    }
    return p2;
}

原文:https://stackoverflow.com/questions/45652154
更新时间:2023-08-27 16:08

最满意答案

这是解决方案的一部分。 您应该能够弄清楚如何实现其余的:

var items = document.getElementsByClassName("item");

Array.prototype.forEach.call(items, function(element) {
  element.addEventListener("mouseover", function() {

    elements = this.querySelectorAll(".qLeft>img");

    Array.prototype.forEach.call(elements, function(el) {
      el.style.boxShadow = "2px 2px 5px -1px black";
    });

    // same for qRight
  });

  element.addEventListener("mouseout", function() {
    // ...
  });
});

Here is part of the solution. You should be able to figure out how to implement the rest:

var items = document.getElementsByClassName("item");

Array.prototype.forEach.call(items, function(element) {
  element.addEventListener("mouseover", function() {

    elements = this.querySelectorAll(".qLeft>img");

    Array.prototype.forEach.call(elements, function(el) {
      el.style.boxShadow = "2px 2px 5px -1px black";
    });

    // same for qRight
  });

  element.addEventListener("mouseout", function() {
    // ...
  });
});

相关问答

更多
  • 没关系,因为如果必须的话,你总是可以将任何DOM元素包装在jQuery对象周围。 var test = document.getElementById('test-table'); // Do some vanilla stuff var jTest = $(test); // Do some jQuery jQuery库建立在JavaScript可用的DOM API之上。 你需要jQuery的唯一原因是做一个复杂的任务,需要花费更多的努力。 在性能方面,差异可以忽略不计。 jQuery添加了支持跨浏览器 ...
  • 您需要使用不同的属性才能访问文档和窗口高度。 document.clientHeight应该是document.body.clientHeight 。 clientHeight属性旨在返回HTML元素的计算高度。 使用body元素适合于该设计。 window.clientHeight应该是window.innerHeight 。 由于window不是HTML元素,因此它有自己的高度属性 。 我还简化了进度条属性设置逻辑。 除非您有一些外部要求来设置data-max和data-value属性,否则可以删除这些 ...
  • 这是解决方案的一部分。 您应该能够弄清楚如何实现其余的: var items = document.getElementsByClassName("item"); Array.prototype.forEach.call(items, function(element) { element.addEventListener("mouseover", function() { elements = this.querySelectorAll(".qLeft>img"); Array. ...
  • 首先请注意,您的jQuery示例比它需要的更复杂。 你可以这样做: $('.bar-toggle').on('click', function() { $('#container').toggleClass("with_toggle", localStorage.toggled != "with_toggle"); localStorage.toggled = localStorage.toggled == "with_toggle" ? '' : 'with_toggle'; }); 要将其转换 ...
  • 简单的答案是,这是不可能的,因为jQuery提供了一个基于香草JS的事件层。 这意味着,香草JS无法与添加的图层进行交谈。 总而言之,jQuery可以捕获vanilla JS事件,但是vanilla JS无法捕获jQuery添加的事件。 The short answer is that this is impossible as jQuery provides an event layer over vanilla JS. That means that vanilla JS cannot talk to ...
  • jQuery在幕后为你做了很多。 等效的纯DOM代码可能如下所示: // Get all header elements var header = document.getElementsByTagName('h1'), parent, newP, text; // Loop through the elements for (var i=0, m = header.length; i < m; i++) { parent = header[i].parentNode; ...
  • 查询文档以使用所需的选择器,在这种情况下: .analytics:not(#promo) 将NodeList转换为数组 使用谓词过滤数组: element => element.querySelector('your-selector') 如果没有找到子元素, element.querySelector('your-selector')将计算为null (这是假的) 一般作为一种功能 function has(nodeList, selector) { return Array.from(nodeLi ...
  • 我发现编写这两个函数在从jQuery转换到本地JS时确实有帮助。 function domEach(selector, handler, context) { return Array.from(document.querySelectorAll(selector), handler, context); } // If you get a TypeError "Array.from" is not a function, use the polyfill // found on MPN. // ...
  • 一种选择是使用CSS选择器以及.querySelectorAll()方法 。 由于CSS具有:not()伪类 ,因此可以使用它来取消script , style和iframe元素。 var elements = document.querySelectorAll('*:not(script):not(style):not(iframe'); 在上面的代码片段中, elements将是包含所选元素集合的NodeList 。 One option would be to use a CSS selector ...
  • 您没有告诉服务器您如何编码请求中的数据。 r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 据推测,您用于处理数据的任何服务器端处理程序都没有正确解析它,因此找不到所需的数据,然后返回一个空白数组作为结果。 You haven't told the server how you are encoding the data in the request. r.setRequestHeader("Content-T ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。