Latest update

- Converting Euler's Angles (ZXZ rotation) to spherical system (theta, phi)
- what is the meaning by words of the following transversality problem (GP 1.5.5)
- Meaning of period symbol in this context?
- Pumping Lemma String Followed By Substring (Prove Language Is Irregular)
- Why the equality $\mathcal O_L/\mathfrak p\mathcal O_L=(\mathcal O_K/\mathfrak p)[\overline\alpha]$ holds?
- Convergence of sample mean using CLT
- Recursive argmin
- What will be the graph of the straight line Arg (z-a)= $\pi$ + $\theta$.
- What is the best way to create an occurrence severity index for the situation below?
- Expected Winnings Problem
- riemann mapping theorem. Why using rotations?
- Calculating two conditional probabilities
- On the Limit of a quotient $\frac{2^n}{e^{P(n)}}$
- Using integration , find the area of the region enclosed by $ x^2=y $ , the line $ y=x+2 $ and the x-axis.
- Integration by parts loop with coefficient
- Why can we conclude $x=0$ or $y=0$ from $\operatorname{det}(M)\neq 0$?
- Upper bound of number of states with $P$-Markov chain.
- Finding the position of a given circle that contains the most randomly placed points
- Showing a function is constant using Schwarz reflection prinbiple
- Conformal mappings are always biholomorphic

# Finding the nth to last node in a linked list

2017-10-17 20:08:34

I have a linked list with n elements and my goal is to find the 'nth to last element' in the list. I would like to present my approach. Please provide your opinions on whether it is optimal or how may I improve it.

Sample linked list "4->5->6->8->2->9->1"

Suppose the question indicates find 3th to last element in the list.

a)In a single pass, get the length of the linked list

b) Determine which node needs to be traversed upto (len(list)-nth element from last-1)

c) Make (len(list)-nth element from last-1) moves from head to get the element

Run time should be O(n)

Thank you