下面是我的函数,它按以下顺序转换链接列表:
示例:
输入: 1->2->3->4->5->6->7->8->NULL,k=3
输出: 3->2->1->6->5->4->8->7->NULL.
void SingleList::orderSort3()
{
// If linked list is empty or there is only one node or two nodes in list
if(getHead() == NULL || getHead()->get
必须是O(n)和就地(空间复杂度为1)。下面的代码可以工作,但是有没有更简单或者更好的方法呢?
public void invert() {
if (this.getHead() == null)
return;
if (this.getHead().getNext() == null)
return;
//this method should reverse the order of this linked list in O(n) time
Node<E> prevNode = this.getHead().get
我有个问题。我想删除一个节点,但是当我打印列表时,它仍然存在。我不知道问题出在哪里。
public void REMOVEWORD(String word) {
//declare and initialize a help pointer
Word helpPtr = head;
while (helpPtr.getNext() != null) {
if (helpPtr.getNext().getWord().equals(word)) {
helpPtr.setNext(helpPtr.getNext().getNext
对于编程课程,我必须使用LinkedList编写一个多项式类。一切正常,但只要两个多项式的项包含0作为幂,(7^0),方法就会冻结。我非常确定问题出现在simplifyPoly()方法中,特别是当它试图将新项添加到新的多项式中时。我不明白为什么会发生这种事。我希望有人能回答这个问题,并告诉我为什么会发生这种情况,或者我如何在明晚之前修复它,因为那是到期的时候!提前谢谢你!
public class Main3{
public static void main(String[]args){
Polynomial poly = new Polynomial();
我正在处理一个问题,我要将一个循环链表一分为二。如果列表是偶数,则拆分将导致两个偶数列表。如果拆分是奇数,则第一个列表将具有额外的节点。
以下是我的节点类的代码
public class CLLNode {
private int data;
private CLLNode next;
public CLLNode(int d)
{
this.data = d;
}
public void setData(int d)
{
this.data = d;
}
public int ge
我需要实现一个LinkedList,到目前为止,我已经编写了我的方法,以便按照列表中的顺序插入值。我有我的Node,前台,作为我的类的实例数据,当我创建我的第一个值,并试图在我的新节点上设置前沿的下一个值时,我会得到一个nullpointerexception。
public class Class1 {
private Node front = null;//Linked List pointer
private int comparisons = 0; //# of comparisons in Lists
//private LinkedList<Node> orderLin
我试图从链接列表中删除一个特定的字符串,但是我一直得到空指针异常错误!有什么帮助吗伙计们?
public void delete(String deleteName) {
LinkedListNode current = head;
if(head==null){
System.out.println("Nothing to delete from the list! Insert data.");
}
// for head
else if(current.getName
所以我已经在标题中说过了,我想从LinkedList中删除最大的值,但我无法理解如何准确地做到这一点。我试过了,但我犯了个错误。
//Remove n from list 'first'
public static void Remove(Node<int> first, Node<int> n)
{
Node<int> pos = first;
while (pos.GetNext() != n)
pos = pos.GetNex
//Node Class
public class MyNode<T extends Comparable<T>> implements INode<T> {
public T key;
public MyNode next;
public MyNode(T key) {
this.key = key;
this.next = null;
}
@Override
public T getKey() {
return key;
}
@Overri
目前,我正在使用java的linkedlist。我们的任务是从头开始创建一个,将用户输入(Strings)按字母顺序排序。我尝试了很多事情,但我想出的概念似乎都没有用。如有任何建议或建议,将不胜感激。
package linkedlist;
public class Linked {
Node head;
int listCount;
public Linked(){
head = new Node(null);
listCount = 0;
}
public void add(String data){
我知道有更好的方法可以用hashSets..etc做到这一点,但我还是想用老方法。这是我写的消除重复项的函数,一个棘手的情况是,如果重复项是最后一个节点,我必须为它写一个特例。我做得对吗,我觉得这太老套了。
// Assume list like this: 0->1->2->3->4->0
//and want to remove 0 in this example
//O(n^2)
public void removeDuplicatesV1(){
Node current, itr;
itr = head;
if(head !=null){
我的职责:
import java.util.Collection;
import java.util.Collections;
public class LinkedList {
private Node head;
private int listCount;
// konstruktor na povrzanata lista
public LinkedList()
{
// ova e prazna lista, pa zatoa 'head' pokazuva kon nov jazok bez eleme
我正在尝试将一个LinkedList添加到另一个LinkedList中。在过去的一两个小时里,我一直在尝试这样做,但我已经束手无策了。这是我主要想要运行的线路。
l.addList(l2); //l is the original list and l2 is the list I am trying to add, l2 does have elements in it.
下面是LinkedList类:
public class LinkedList<T>
{
private Node<T> head; // head of the list always at
我已经编写了以下代码来计算LinkedList中所有偶数条目的总和。然而,由于我使用(n.getNext).getNext()的代码行,我一直得到一个NullPointerException。
有没有人能告诉我为什么会这样?
下面是我引用的代码片段:
public int sumEven() {
return sumEven(head);
}
// private sumEven helper
private int sumEven(IntListNode n) {
int nodeNumber=1;
int count=0;
if(n.getNext() =
我正在做一个学习练习,并用迭代器创建我自己的链表。类如下所示:
public class LinkedList<T> implements Iterable <T> {
private Node<T> head;
private Node<T> tail;
private int size;
public LinkedList() {
head = new Node<T>();
tail = new Node<T>();
head.setNe
请回顾我的执行情况。我知道Java已经有了一个实现,但我只是想提出我的实现。如果我能改进的话请告诉我。
import java.util.NoSuchElementException;
public class LinkedListImplementation<T> {
private int size;
private ListNode<T> head;
static class ListNode<T> {
private T value;
private ListNode<T> ne
我正在寻找关于使用Java实现Queue、Stack和LinkedList的反馈。我将非常感谢反馈,我可以改进和任何错误,我所犯的。
LinkedList
public class LinkedList<T> {
private int size;
private Node<T> head;
public LinkedList() {
this.head = null;
this.size = 0;
}
public int size() {
return size;
我必须编写一个程序来实现学生的链接列表,在本例中,Student是一个单独的类,将被调用。我的链表应该能够找到一个使用该学生名字的学生,这是我的程序失败的地方,它似乎找不到那个学生的节点,因此也不会删除。我正在为一个类写这篇文章,但是教授不允许使用内置的链表类。
这是我的代码
public class StudentList {
StudentNode head;
public StudentList() {
shead = null;
}
public void setShead(StudentNode data) {
我正在尝试删除给定索引上的节点,但我一直没有通过测试,有时甚至会得到一个NullPointerException。以下是我迄今所做的工作:
public void remove(int pos)
{
if (head == last)
{
head = null;
last = null;
}
if (pos == size - 1)
return removeLast();
if (pos == 0)
return re
我正在编写LinkedList数据类型的实现,但在包含__contains__、__iterate__、__delitem__、__str__和__repr__函数时遇到了错误。
class LinkedList:
# The __Node class is used internally by the LinkedList class. It is
# invisible from outside this class due to the two underscores
# that precede the class name. Python mangles n
我如何将我的add和remove方法从普通的链接到循环链表。使用这段代码,我假设我需要一个尾部引用,等等?
public void add(int index, Object item)
throws ListIndexOutOfBoundsException {
//our index needs to go in along with our stuff in item
Node temp = new Node(item);
Node current = head;
for(int i =
我自己创建了一个单链接列表的实现。在效率方面,还有什么可以改进的吗?另外,您还建议我尝试和实现哪些其他方法。我的目标是理解基本数据结构的组件。
LinkedList:
public class StratchLinkedList {
private Node head;
private int size;
public StratchLinkedList() {
head = null;
size = 0;
}
public void add(Object data) {
Node temp = new Node(data);
Node curr = h