简体   繁体   中英

How to inserting first node for singly linked list in Java?

i just try like that but no respons

 //addFirst
    public void addFirst(Object info) {
        MyNode newNode = new MyNode(info);
        if (head == null) {
            head = newNode;
        } else {
            MyNode cur = head;
            while (cur.getNext() == null) {
                cur = null;
            }
            newNode.setNext(head);
        }
        count = count + 1;
    }

There is the pseudo-code to implement the function you're looking for:

addFirst(obj):
    node = new node(obj)
    node.next = head
    head = node
;

It's up to you to translate it into Java code, if you don't mind ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM