【问题标题】:in a link list what is the difference between head=something; and head.next=something; [closed]在链接列表中 head=something; 之间有什么区别?和 head.next=something; [关闭]
【发布时间】:2018-06-30 00:38:19
【问题描述】:

节点定义为

class Node {
    int data;
    Node next;
}

Node RemoveDuplicates(Node head) {

    Node temp=head;
    while(temp.next!=null){
        if(temp.data == temp.next.data){
            temp.next=temp.next.next;
        } else { 
            System.out.println(temp.data);
            temp=temp.next;
        }
    }
    return head;
}

【问题讨论】:

  • 你的问题是什么?

标签: java linked-list


【解决方案1】:

head 是列表中的第一个值,而 head.next 是第二个值。

例如,如果我的列表是 1->2->3,我的 head 将是 1,而我的 head.next 将是 2。 因此,分配head = 5 会将列表变为5->2->3,这与分配head.next = 5 会将列表变为1->5->3 不同。

【讨论】:

    【解决方案2】:

    head 大概是您的 LinkedList 类的成员(您没有显示)。它是对Node 对象的引用,该对象是列表中的第一个对象。声明

    head = something;
    

    假定something 也是Node 引用,并将something 的值分配给head。另一种说法是something 指向的任何东西现在也被head 指向。

    表达式head.next 表示查看head 指向的Node 并返回next 成员的值(这也是Node 的引用)。在作业的左侧,如

    head.next = something_else;
    

    意思是取something_else的值(也必须是Node的引用),赋值给head当前指向的节点的next成员。

    【讨论】:

      猜你喜欢
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 2015-04-11
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多