2015-10-14 54 views
-1

如何找到並從LinkedList只有一個元素,它的刪除節點?它沒有任何尾巴,當前,頭部,預告片和計數器。如何定位並從一個只有一個元素的LinkedList中移除一個節點,它的頭部?

我也想

  • 拋出NullPointerException如果列表爲空
  • 回報如果提供的價值被發現,去除
  • 回報如果提供的值不找到
Public class SLList 
{ 

    Private class Node 
    { 
     Private int info; 
     Private Node next; 
     Private Node (int value, Node ptr) 
     { 
      Info = value; 
      Next = ptr; 
     } 
    } 

    Protected Node head = null; 

    Public Boolean remove (int value) throws NullPointerException 
    { 

    } 
} 
+1

'Private' or'private'? – Rustam

+1

歡迎來到'java' – Rustam

+0

private是正確的 –

回答

1

如果只有head存在,則表示head.next爲null,請在remove()內檢查它:

if (head != null){ 
     if (head.next == null){ 
      // only head exists - remove it 
      head = null; 
     } 
     else { 
      // remove other nodes normally 
     } 
    } 
+0

謝謝,但正如它在問題中提到的,它不應該有任何計數器! –

相關問題