Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
n th to last element
//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 LinkedListAlgorithms { partial class LinkedListStack { //Find nth to last element of a singly linked list implemented as a stack //1 returns the last object in the linked list public object FindNToLast(int x) { if (head == null) throw new Exception("Empty linked list"); if (x < 1) throw new Exception("Index out of range"); Node lag = head; Node lead = head; //Move the lead pointer up (x-1 positions) while leaving the lag pointer behind for (int i = 1; i < x; i++) lead = lead.Next; //Move the lag and lead pointers up one at a time until the lead is at the end of the list while (lead.Next != null) { lag = lag.Next; lead = lead.Next; } return lag.Data; } } }
run
|
edit
|
history
|
help
0
Enum, TryParse, cast to enum
BC150401319
Enigme
Project Euler Problem 11
Moon Gravity
multiplyarr
Code challenge: Integer Comparison - Overkill - Function Dictionary
Derived static
Decimal Test2
SortRank