Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
nthFromLastElement
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Node { public int data; public Node child; public Node (int data, Node child ){ data = data; child = child; } } public class Program { public static void Main(string[] args) { Node current = new Node(1, null); for (int i = 2; i < 8; i++) { current = new Node(i, current); } Node head = current; // head = 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> (null) Node current2 = new Node(4, null); for (int i = 3; i > 0; i--) { current2 = new Node(i, current2); } Node head2 = current2; // head2 = 1 -> 2 -> 3 -> 4 -> (null) //Node return = nthFromLast(head, 1); //Console.WriteLine(return.data); // nthFromLast(head, 1) should return 1. // nthFromLast(head, 5) should return 5. // nthFromLast(head2, 2) should return 3. // nthFromLast(head2, 4) should return 1. // nthFromLast(head2, 5) should return null. // nthFromLast(null, 1) should return null. } public static Node nthFromLast( Node head, int n) { if( head == null ) return null; Node p = head; Node q = head; for (int i=0; i<n; i++ ){ if( q == null ) return null; q = head.child; } while ( q != null ) { q = q.child; p = p.child; } return p; } } }
run
|
edit
|
history
|
help
0
Nuzrath 284
Quadratic equation
Date Comparison
p link gener tor
Watch Jurassic World Dominion Online Free
COMPLEX NUMBER
random number generator using array
string + null
12
IF... ELSE