list(LinkedList).head 和 Node head 的区别?

Difference between list(LinkedList).head and Node head?

我试图使用 Node head 删除链表的第一个节点,但是当我使用 list.head?

时它不起作用
import java.util.*;

    // Java program to implement 
    // a Singly Linked List 
    public class LinkedList { 

        Node head; 
    // head of list 

        // Linked list Node. 
        // This inner class is made static 
        // so that main() can access it 
        static class Node { 

            int data; 
            Node next; 

            // Constructor 
            Node(int d) 
            { 
                data = d; 
                next = null; 
            } 
        } 

         static void delete(LinkedList list,int x){
            Node curr=list.head,prev=list.head;
            if(curr.data==x&&curr!=null){
                list.head=curr.next;
                return ;
            }
            while(curr.data!=x&&curr.next!=null){
                prev=curr;
                curr=curr.next;
            }
            if(curr.data==x)
                prev.next=curr.next;
            return ;
        }

            // There is method 'insert' to insert a new node 

        // Driver code 
        public static void main(String[] args) 
        { 
            /* Start with the empty list. */
            LinkedList list = new LinkedList(); 
            list = insert(list, 1); 
            list = insert(list, 2); 
            list = insert(list, 3);
                    list = insert(list, 4);
                    delete(list,1);
                    printList(list);
                    //There is method to print list

        } 
    } 
    //Output : 2 3 4

当我使用上面的代码时,我可以删除第一个节点,但是当我使用这段代码时,它不起作用

import java.util.*;

// Java program to implement 
// a Singly Linked List 
public class LinkedList { 

    Node head; 
// head of list 

    // Linked list Node. 
    // This inner class is made static 
    // so that main() can access it 
    static class Node { 

        int data; 
        Node next; 

        // Constructor 
        Node(int d) 
        { 
            data = d; 
            next = null; 
        } 
    } 

     static void delete(Node head,int x){
        Node curr=head,prev=head;
        if(curr.data==x&&curr!=null){
           head=curr.next;
            return ;
        }
        while(curr.data!=x&&curr.next!=null){
            prev=curr;
            curr=curr.next;
        }
        if(curr.data==x)
            prev.next=curr.next;
        return ;
    }

        // There is method 'insert' to insert a new node 

    // Driver code 
    public static void main(String[] args) 
    { 
        /* Start with the empty list. */
        LinkedList list = new LinkedList(); 
        list = insert(list, 1); 
        list = insert(list, 2); 
        list = insert(list, 3);
                list = insert(list, 4);
                delete(list.head,1);
                printList(list);
                //There is method to print list

    } 
} 
//Output: 1 2 3 4

我想知道这些是同一个东西是不同的,Node head 和 list(LinkedList).head

注意:这两种方法都适用于其他节点,不同之处仅在于第一个节点。

在第一个中,您将列表作为输入传递,在第二个中,您将引用您的头节点,如果您在第一个示例中注意到,如果数据最初存在,您正在修改列表的头 node.Here'这是执行此操作的代码片段。

 Node curr=list.head,prev=list.head;
            if(curr.data==x&&curr!=null){
                list.head=curr.next;
                return ;
            }

但是在你的第二个例子中,如果在第一个节点找到数据,那么你正在将 curr.next 分配给方法本地的 head 变量,这样列表的 head 值保持不变,当你尝试在 main 中打印列表时方法再次显示旧头。这是第二个示例的代码片段

Node curr=head,prev=head;
        if(curr.data==x&&curr!=null){
           head=curr.next;
            return ;
        }

因此,如果您将头指针存储在 LinkedList 对象中,那么您必须修改其中的值。