链表 - 获取第N个节点【难度:1级】:
答案1:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
if ( node== null )
throw new ArgumentException ( ) ;
if ( index== 0 )
return node;
else
{
return GetNth ( node. Next, index- 1 ) ; }
}
}
答案2:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
if ( node== null ) {
throw new ArgumentException ( ) ;
}
if ( index == 0 )
return node;
else
return GetNth ( node. Next, index - 1 ) ;
}
}
答案3:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
if ( index < 0 || node == null )
{
throw new ArgumentException ( ) ;
}
else
{
int currentIndex = 0 ;
Node currentNode = node;
while ( currentNode != null )
{
if ( currentIndex == index)
{
return currentNode;
}
else
{
currentIndex++ ;
currentNode = currentNode. Next;
}
}
throw new ArgumentException ( ) ;
}
}
}
答案4:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static int Length ( Node head)
{
int length = 0 ;
Node currentNode = head;
while ( currentNode != null )
{
length++ ;
currentNode = currentNode. Next;
}
return length;
}
public static Node GetNth ( Node node, int index)
{
Node nodeToReturn = node;
if ( node == null || 0 > index || index > Node. Length ( node) - 1 )
{
throw new ArgumentException ( ) ;
}
else
{
for ( int i = 0 ; i < index; i++ )
{
nodeToReturn = nodeToReturn. Next;
}
return nodeToReturn;
}
}
}
答案5:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
while ( node != null & amp; & amp; index > 0 ) {
node = node. Next;
index-- ;
}
if ( node == null || index != 0 )
throw new ArgumentException ( ) ;
return node;
}
}
答案6:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
int i = 0 ;
while ( i!= index)
{
if ( node== null ) throw new ArgumentException ( ) ;
else { ++ i; node = node. Next; }
}
if ( node== null ) throw new ArgumentException ( ) ;
return node;
}
}
答案7:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
var current = node;
var counter = 0 ;
while ( current != null )
{
if ( counter++ == index)
{
return current;
}
current = current. Next;
}
throw new ArgumentException ( ) ;
}
}
答案8:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
var current = node;
var count = 0 ;
while ( current != null )
{
if ( count == index)
return current;
current = current. Next;
count++ ;
}
throw new ArgumentException ( ) ;
}
}
答案9:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
if ( node == null || index < 0 )
{
throw new ArgumentException ( ) ;
}
var tempNode = node;
for ( int i = 0 ; i < index; i++ )
{
if ( tempNode. Next != null )
{
tempNode = tempNode. Next;
}
else
{
throw new ArgumentException ( ) ;
}
}
return tempNode;
}
}
答案10:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
if ( node == null ) throw new ArgumentException ( ) ;
var nextNode = node;
int indexRes = 0 ;
while ( nextNode != null )
{
if ( indexRes == index)
{
return nextNode;
}
indexRes++ ;
nextNode = nextNode. Next;
}
throw new ArgumentException ( ) ;
}
}
答案11:
using System;
public partial class Node
{
public int Data;
public Node Next;
public Node ( int data)
{
this . Data = data;
this . Next = null ;
}
public static Node GetNth ( Node node, int index)
{
while ( index > 0 & amp; & amp; node != null ) {
node = node. Next;
index-- ;
}
if ( node == null || index != 0 ) {
throw new System. ArgumentException ( "Invalid index" , "index" ) ;
}
return node;
}
}