list(LinkedList).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

我想知道这些是同一件事是不同的,节点头和列表(链接列表(.head

注意:这两种方法都适用于其他节点,区别仅在于第一个节点。

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

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

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

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

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

最新更新