Add to front of linked list

让人想犯罪 __ 提交于 2019-12-09 18:18:47

问题


I am confused as to how to add to the front of the linked list.

/**
* data is added to the front of the list
* @modifies this
* @ffects 2-->4-->6 becomes data-->2-->4-->6
*/
public void insert(E data) {
    if (front == null) 
        front = new Node(data, null);
    else {
        Node temp = new Node(data, front);
        front = temp;
    }
}

This creates a cycle. How do I avoid that?

I have a LinkedList class which holds the front Node, in a variable called front. I have a Node class within this LinkedList class.

Any help would be appreciated. Thank you.


回答1:


Don't you have access to "Next" node ?

In that case

public void insert(E data) {
    if (front == null) { 
        front = new Node(data, null);
    } else {
        Node temp = new Node(data, null);
        temp.next = front;
        front = temp;
    }
}

--

 class LinkedList {
    Node front;

    LinkedList() { 
        front = null; 
    }

    public void AddToFront(String v) {
        if (front == null) {
            front = new Node(v);
        } else {
            Node n = new Node(v);
            n.next = front;
            front = n;
        }
    }   
}

class Node {
    public Node next;
    private String _val;

    public Node(String val) {
        _val = val;
    }
}



回答2:


I'm assuming that the Node constructor takes a next pointer as its 2nd argument, in which case I don't see anything obvious wrong with this code. This really sounds like a homework question. If it is, you should tag it as such.




回答3:


With my limited linked list knowledge, I would venture this:

Node temp = new Node(data);
temp.next = front;
front = temp;

You might want to wait around for somebody to confirm though.




回答4:


This creates a cycle. How do I avoid that?

It is not possible to know for sure without the rest of the code for your linked list implementation, but the code that you have supplied doesn't look like it creates a cycle at all.

If a cycle is being created, it is most likely being created elsewhere. Alternatively, you / your tests are misdiagnosing some other failure as being caused by a cycle.

If you need more help, post more code / evidence ... particularly the Node constructor, and the code that makes you think you have a cycle.




回答5:


Add a new node and if the current head is not null, then point the current head to the newly created node as the next node.

Node insert(Node head,int x) {
    Node node = new Node();
    node.data = x;
    if(head != null) {
       node.next = head;}
    return node;
}



回答6:


This is my Implementation of Inserting a node to front or head of the Linked List in Java.

void insertAtHead(Object data){
    if(head==null) {
        head = new Node(data);
    }
    Node tempNode = new Node(data);
    Node currentNode = head;
    tempNode.setNext(currentNode.getNext());
    head.setNext(tempNode);
    incrementCounter();
}



回答7:


A simple and quick [ may not be efficient ] solution is to create a temporary new LinkedList with the new element and merge the two lists together with the temp-list in the front. see the example below

import java.util.*;
public class Main
{

    public static Queue<Integer> addFirst(Queue<Integer> intQueue, Integer i){
        Queue<Integer> intQueue2 =  new LinkedList<Integer>();
        intQueue2.add(i);
        intQueue2.addAll(intQueue);
        intQueue = intQueue2;
        return intQueue;
    }

    public static void main(String[] args) {
        System.out.println("Hello LinkedList");

        Queue<Integer> intQueue =  new LinkedList<Integer>();
        intQueue.add(3);
        intQueue.add(4);
        intQueue.add(5);

        intQueue = addFirst(intQueue,2);
        intQueue = addFirst(intQueue,1);


        System.out.println(intQueue);
    }
}


来源:https://stackoverflow.com/questions/4882857/add-to-front-of-linked-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!