2017-03-15 1 views
1

여기에 질문이 있습니다. 지금은 마법을 수행참조 또는 값으로 연결된 목록?

ListNode newHead = head; 
newHead = head.next.next; 
//Now newHead is pointing to (3) in the linked list. 

:

1 -> 2 -> 3 -> 4 

내가 그런 짓을 :

/** 
* Definition for singly-linked list. 
* public class ListNode { 
*  int val; 
*  ListNode next; 
*  ListNode(int x) { val = x; } 
* } 
*/ 

링크 된 목록을 고려 : (자바) 다음과 같이 연결리스트가 구현되는 말

newHead.val = 87 

linke D리스트가된다 :

1 -> 2 -> 87 -> 4 

내가 머리 및 newHead하지를 인쇄합니다.

왜 이런가요? 나는 머리를 가진 무엇이든을 변경하지 않으며 그러나 아직도 변화했다?

+0

분명히, 그것은 당신이 같은 물건을 바꾸고 있기 때문입니다. ListNode에는 각 ListNode에 대한 참조 만 있습니다. –

+0

@MatthiasFax, 내가 수업을 만들고 나서 이렇게하면 언제나 좋아할까요? – sssszzzzzzssszzz

+1

예, 객체를 복제하거나 완전 복사하지 않으면 프리미티브를 제외하고 값에 대한 참조 만 있습니다. 기존 질문을 확인하십시오. http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value http://stackoverflow.com/questions/4600974/passing-primitive -data-by-reference-in-java –

답변

0

그래서 당신은이를 사용할 수 있습니다

노드 클래스 :

public class IntNode { 

    int value; 
    IntNode next; 

    public IntNode(int value) { 
     this.value = value; 
    } 
} 

단일 연결리스트 클래스 :

/** 
* A singly-linked list of integer values with fast addFirst and addLast methods 
*/ 
public class LinkedIntList { 

    IntNode first; 
    IntNode last; 
    int size; 

    /** 
    * Return the integer value at position 'index' 
    */ 
    int get(int index) { 
     return getNode(index).value; 
    } 

    /** 
    * Set the integer value at position 'index' to 'value' 
    */ 
    void set(int index, int value) { 
     getNode(index).value = value; 
    } 

    /** 
    * Returns whether the list is empty (has no values) 
    */ 
    boolean isEmpty() { 
     return size == 0; 
    } 

    /** 
    * Inserts 'value' at position 0 in the list. 
    */ 
    void addFirst(int value) { 
     IntNode newNode = new IntNode(value); 
     newNode.next = first; 
     first = newNode; 
     if(last == null) 
      last = newNode; 
     size++; 
    } 

    /** 
    * Appends 'value' at the end of the list. 
    */ 
    void addLast(int value) { 
     IntNode newNode = new IntNode(value); 
     if(isEmpty()) 
      first = newNode; 
     else 
      last.next = newNode; 

     last = newNode; 
     size++; 
    } 

    /** 
    * Removes and returns the first value of the list. 
    */ 
    int removeFirst() { 
     if(isEmpty()) { 
      System.out.println("RemoveFirst() on empty list!"); 
      System.exit(-1); 
     } 

     int value = first.value; 
     if(first == last) { 
      // List has only one element, so just clear it 
      clear(); 
     } 
     else { 
      first = first.next; 
      size--; 
     } 

     return value; 
    } 

    /** 
    * Removes and returns the last value of the list. 
    */ 
    int removeLast() { 
     if(isEmpty()) { 
      System.out.println("RemoveLast() on empty list!"); 
      System.exit(-1); 
     } 

     int value = last.value; 
     if(first == last) { 
      // List has only one element, so just clear it 
      clear(); 
     } 
     else { 
      // List has more than one element 
      IntNode currentNode = first; 
      while(currentNode.next != last) 
       currentNode = currentNode.next; 

      currentNode.next = null; 
      last = currentNode; 
      size--; 
     } 
     return value; 
    } 

    /** 
    * Removes all values from the list, making the list empty. 
    */ 
    void clear() { 
     first = last = null; 
     size = 0; 
    } 

    /** 
    * Returns a new int-array with the same contents as the list. 
    */ 
    int[] toArray() { 
     int[] array = new int[size]; 
     int i = 0; 
     for(IntNode n = first; n != null; n = n.next, i++) 
      array[i] = n.value; 
     return array; 
    } 

    /** 
    * For internal use only. 
    */ 
    IntNode getNode(int index) { 
     if(index < 0 || index >= size) { 
      System.out.println("GetNode() with invalid index: " + index); 
      System.exit(-1); 
     } 

     IntNode current = first; 
     for(int i = 0; i < index; i++) 
      current = current.next; 
     return current; 
    } 
} 

이 설명은 코드의 주석을 참조하십시오.

관련 문제