2017-04-25 7 views
0

어떻게 부분 문자열 방법을 사용할 수 있습니까? copy = copy.next = new Node(curr.data);링크 된 목록의 부분 문자열 방법과 관련된 문제

public LL substring(int startPosition) { 

    if (startPosition < 0) 
     throw new IndexOutOfBoundsException(); 

    LL substring = new LL(); 
    Node curr = first; 
    for (int i = 0; i < startPosition; i++) { 
     curr = curr.next; 
    } 
    Node copy = new Node(curr.data); 
    substring.first = copy; 

    while (curr != null && copy != null) { 
     curr = curr.next; 
     copy = copy.next = new Node(curr.data); 
    } 
    return substring; 
} 
+0

'curr.next'는'null' 될 수있다, 그래서 NLP는'curr.data'에있다 :

나는이 줄에 널 포인터 예외가 점점 계속 – Oswald

답변

0
public LL substring(int startPosition) { 
    if (startPosition < 0) 
     throw new IndexOutOfBoundsException(); 

    LL substring = new LL(); 
    Node curr = first; 
    for (int i = 0; i < startPosition; i++) { 
     if (curr == null) { 
      throw new IndexOutOfBoundsException(); 
     } 
     curr = curr.next; 
    } 
    Node prev = null; 
    while (curr != null) { 
     Node copy = new Node(curr.data); 
     if (prev == null) { 
      substring.first = copy; 
     } else { 
      prev.next = copy; 
     } 
     prev = copy; 
     curr = curr.next; 
    } 
    return substring; 
}