2015-10-14 57 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

谢谢,但正如它在问题中提到的,它不应该有任何计数器! –

相关问题