Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
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
Please
log in
to post a comment.
AKASH B AND HIS CODES\\
convert a string of numbers to a list then use a forEach to loop through the list
recursive solution for subset sum
COMPLEX NUMBER
Kumaran-RegText-CityStatePostalCode Parsing
Determine if a string has all unique characters.
merge sort
Find if the given word occurs in the string necessarily in that order.
SIN_in_Pow
Int to 4 bytes
stackse - search stackoverflow differently
Please log in to post a comment.