Thursday, 2 August 2012

C program to return the nth node from the end of a linked list.

Suppose one needs to get to the 6th node from the end in this LL. First, just keep on incrementing the first pointer (ptr1) till the number of increments cross n (which is 6 in this case)


STEP 1 : 1(ptr1,ptr2) -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10

STEP 2 : 1(ptr2) -> 2 -> 3 -> 4 -> 5 -> 6(ptr1) -> 7 -> 8 -> 9 -> 10



Now, start the second pointer (ptr2) and keep on incrementing it along with the first pointer (ptr1) until first pointer(ptr1) reaches the end of the LL.


STEP 3 : 1 -> 2 -> 3 -> 4(ptr2) -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 (ptr1)


So here you have  the 6th node from the end pointed to by the ptr2




c code for the above algorithm:

struct node
{
int data;
struct node *next;
}mynode;


mynode * nthNode(mynode *head, int n /*pass 0 for last node*/)
{
mynode *ptr1,*ptr2;
int count;

if(!head)
{
return(NULL);
}

ptr1 = head;
ptr2 = head;
count = 0;

while(count < n)
{
count++;
if((ptr1=ptr1->next)==NULL)
{
//Length of the linked list less than n. Error.
return(NULL);
}
}

while((ptr1=ptr1->next)!=NULL)
{
ptr2=ptr2->next;
}

return(ptr2);
}


88 comments:

  1. Hi thеre! Someone in my Facebook group shаred thiѕ websіte with
    uѕ so I came to check іt οut. І'm definitely loving the information. I'm bοok-marking and wіll
    bе twеeting thіѕ to mу followers!
    Wοnderful blog and fantаstic design and style.

    ReplyDelete
  2. I do аgree with all of the ideаѕ yоu've presented to your post. They'ге
    really convincing anԁ can definitelу ωork.
    Still, the posts are too brіef fοr staгters.
    Could you pleaѕe prolong them a bit fгom ѕubsequent time?

    Thanks fοr the poѕt.

    my wеbsitе - Arjun Kanuri

    ReplyDelete
  3. Very nice aгtіcle, exaсtly what
    I neеded.

    my web site ... reputation management

    ReplyDelete
  4. It's a pity you don't have a donate button! I'd certainly donate to this superb blog! I guess for now i'll settle for book-marking and adding your RSS feed to my Google
    account. I look forward to new updates and will talk about this
    website with my Facebook group. Talk soon!

    my weblog ... Justin Sather

    ReplyDelete
  5. It is nоt my first time to paу a quicκ viѕit this web sitе, i am brοwsing thіѕ wеb pаge dаilly and gеt good information from
    here everyԁay.

    Also visit my website: Lloyd Irvin

    ReplyDelete
  6. When ѕοmеone writes an рaragraph hе/she maіntains
    the idеa οf а usеr in his/heг braіn that hoω a usеr can unԁeгstаnԁ it.
    Тhus that's why this paragraph is amazing. Thanks!

    Here is my web blog ... reputation management

    ReplyDelete
  7. Howdy! This post couldn't be written much better! Looking at this post reminds me of my previous roommate! He continually kept talking about this. I most certainly will send this post to him. Fairly certain he will have a great read. I appreciate you for sharing!

    Feel free to visit my weblog - tasers for women

    ReplyDelete
  8. I have been browsing onlіne more than 3 hourѕ tоdаy, уet I
    neνer founԁ any іntегeѕting аrticle
    like yours. Ӏt iѕ pretty value enοugh for me.
    In my opіnion, іf all webmastеrs anԁ blоggeгs made goоd content as yοu probably ԁid, the net wіll likely be a lot
    morе uѕeful than ever before.

    kate dircksen

    ReplyDelete
  9. I will right away snatch your rss as I can't to find your
    e-mail subscription hyperlink or newsletter service.
    Do you've any? Please let me know in order that I could subscribe.
    Thanks.

    Also visit my website - lee trotman sce

    ReplyDelete
  10. Thank you all for your valuable feedback. just now added rss feed !

    ReplyDelete
  11. Write more, thats all I have to say. Literally, it seems
    as though you relied on the video to make your
    point. You clearly know what youre talking about, why throw away your intelligence
    on just posting videos to your weblog when you could be giving us something informative to
    read?

    nino tinari

    ReplyDelete
  12. Hi to all, it's truly a fastidious for me to pay a visit this
    site, it includes precious Information.

    Lee Trotman

    ReplyDelete
  13. First of all I want to say fantastic blog! I had a quick question that I'd like to ask if
    you don't mind. I was interested to find out how you center
    yourself and clear your thoughts before writing. I have had a tough time clearing my mind
    in getting my thoughts out. I truly do take pleasure in writing but it just seems like the
    first 10 to 15 minutes are wasted just trying to figure
    out how to begin. Any suggestions or hints? Cheers!

    my blog post :: Eric Gray

    ReplyDelete
  14. I delight in, cause I found exactly whqt I used to be havingg a look for.
    You have endeed myy folur day long hunt! God Bless you
    man. Have a grat day. Bye

    Derwick

    ReplyDelete
  15. If some one needs expert view regarding blogging then i advise him/her to visit this weblog, Keep up the
    good job.

    My blog post eric gray

    ReplyDelete
  16. Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn't appear.
    Grrrr... well I'm not writing all that over again.
    Anyway, just wanted to say superb blog!

    Here is my weblog ... telefonkatalogen.biz

    ReplyDelete
  17. There is certainly a lot to learn about this subject.
    I love all the points you have made.

    Nino Tinari

    ReplyDelete
  18. Wow! At last I got a web site from where I be able to genuinely get helpful information concerning my study and
    knowledge.

    Feel free to visit my web site: terry bandy

    ReplyDelete
  19. We are a group of volunteers and oppening a neww scheme in
    our community. Your website provided us with valuable info to work on.
    You've done a formidable job and our entire community will be grateful to you.


    Francisco Convit Guruceaga

    ReplyDelete
  20. This paragraph will help the internet viewers for setting up
    new blog or even a blog from start to end.

    Here is my weblog - USAF ornament

    ReplyDelete
  21. Saved as a favorite, I love your web site!

    Also visit my blog post :: adeles watch

    ReplyDelete
  22. Article writing is also a excitement, if you be
    familiar with after that you can write if not it is complex to write.



    Have a look at my blog post ... florida gators bracelet

    ReplyDelete
  23. It's not my first time to visit this website, i am visiting
    this website dailly and get nice information from here all the time.


    Pedro Trebbau López

    ReplyDelete
  24. I constantly emailed this webpage post page to all my friends,
    because if like to read it next my friends will too.


    my blog air force ornament

    ReplyDelete
  25. Today, I went to the beach front with my children. I found a sea shell and gave it to
    my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her ear. She never wants to go back!
    LoL I know this is entirely off topic but I had to tell someone!


    Feel free to surf to my homepage star of david charm

    ReplyDelete
  26. Wow, amazing blog layout! How lengthy have
    you been running a blog for? you made running a blog look easy.

    The entire look of your web site is fantastic, let
    alone the content!

    Also visit my website :: eric gray buffalo ny

    ReplyDelete
  27. Thank you a bunch for sharing this with all of us you really understand what you're speaking about!

    Bookmarked. Kindly also consult with my website =).
    We will have a link change arrangement between us

    Look into my blog post ... Eric Gray Buffalo NY

    ReplyDelete
  28. Asking questions are truly nice thing if you are not
    understanding anything totally, but this paragraph provides fastidious understanding yet.


    My blog post: Leopoldo Alejandro Betancourt López

    ReplyDelete
  29. These are in fact impressive ideas in concerning blogging.
    You have touched some fastidious points here. Any way keep up wrinting.


    Feel free to surf to my web page ideas-for-picking-low-cost-lad

    ReplyDelete
  30. If some one wishes expert view on the topic of running a blog afterward i propose him/her to
    pay a visit this webpage, Keep up the nice job.

    My web blog - Leopoldo Alejandro Betancourt Lopez

    ReplyDelete
  31. I like what you guys tend to be up too. This type of clever work and exposure!
    Keep up the excellent works guys I've added you guys to my personal blogroll.


    Feel free to visit my site Derwick Associates

    ReplyDelete
  32. I’m not that much of a internet reader to be honest but your blogs really nice, keep it up!

    I'll go ahead and bookmark your website to come back
    later on. Cheers

    my site: Leopoldo Alejandro Betancourt López

    ReplyDelete
  33. You could definitely see your enthusiasm in the work you write.
    The sector hopes for even more passionate writers such
    as you who are not afraid to say how they believe. All the time follow
    your heart.

    Have a look at my homepage: cz wedding set

    ReplyDelete
  34. Good post. I learn something new and challenging on websites I stumbleupon
    on a daily basis. It will always be useful to read through articles from other
    writers and practice a little something from their web sites.


    Feel free to visit my blog; autism pin

    ReplyDelete
  35. I loved as much as you'll receive carried out right here.
    The sketch is tasteful, your authored material stylish. nonetheless, you
    command get bought an nervousness over that you wish be delivering the following.
    unwell unquestionably come more formerly again since exactly the same nearly very often
    inside case you shield this hike.

    Also visit my homepage Derwick Associates

    ReplyDelete
  36. It's remarkable to visit this website and reading the views of
    all friends about this paragraph, while I am
    also keen of getting experience.

    My page; Eric Gray Buffalo NY

    ReplyDelete
  37. Wow! Finally I got a web site from where I be capable of genuinely obtain useful information concerning my study and
    knowledge.

    silver heart necklace

    ReplyDelete
  38. Hi, all is going perfectly here and ofcourse every one is sharing information, that's actually excellent, keep up writing.


    Also visit my homepage: doug pitassi

    ReplyDelete
  39. Thank you for sharing your info. I truly appreciate your efforts and I will be waiting for your further write
    ups thanks once again.

    my site: doug pitassi

    ReplyDelete
  40. What's Going down i am new to this, I stumbled upon this I've found It
    absolutely helpful and it has aided me out loads. I'm hoping
    to give a contribution & help different customers like its helped me.
    Good job.

    doug pitassi

    ReplyDelete
  41. Thanks to my father who told me regarding this website,
    this web site is actually remarkable.

    Doug Pitassi

    ReplyDelete
  42. naturally like your web-site but you have to check the spelling on quite
    a few of your posts. A number of them are rife with spelling problems and I find it very troublesome to tell
    the truth nevertheless Iཿll surely come back again.

    Feel free to visit my webpage; book of ra kostenlos

    ReplyDelete
  43. Hey there, You have done a great job. I will certainly digg it and personally suggest to my
    friends. I'm confident they'll be benefited from this website.


    silver chain

    ReplyDelete
  44. Its such as you learn my mind! You appear to know so much approximately this, such as
    you wrote the ebook in it or something. I think that you
    simply could do with a few percent to drive the message home a
    little bit, however other than that, that is fantastic blog.
    A great read. I will certainly be back.

    Feel free to visit my blog :: eric gray buffalo ny

    ReplyDelete
  45. Touche. Great arguments. Keep up the great work.


    Here is my blog post :: eric gray buffalo ny

    ReplyDelete
  46. Good way of explaining, and pleasant piece of writing to obtain facts
    on the topic of my presentation subject, which i am going
    to deliver in institution of higher education.

    Stop by my site: University of Kentucky watch

    ReplyDelete
  47. bookmarked!!, I really like your blog!

    Also visit my blog post: silver heart necklace

    ReplyDelete
  48. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way,
    how could we communicate?

    Here is my website: Eric Gray Buffalo NY

    ReplyDelete
  49. it's good to hear from all of you. Thanks :)

    ReplyDelete
  50. Hello, after reading this amazing piece of writing i am also delighted to share my familiarity here with colleagues.



    eric gray buffalo ny

    ReplyDelete
  51. My brother suggested I would possibly like this website. He was once totally right.
    This put up actually made my day. You cann't imagine simply how so much time
    I had spent for this information! Thanks!


    eric gray buffalo ny

    ReplyDelete
  52. Unquestionably imagine that which you said. Your
    favourite justification appeared to be at the internet the easiest thing
    to remember of. I say to you, I certainly get annoyed whilst people think about worries that they plainly do not recognize about.

    You managed to hit the nail upon the top as well as outlined out the whole thing without having side effect ,
    people can take a signal. Will probably be back to get
    more. Thank you

    eric gray buffalo ny

    ReplyDelete
  53. This is really fascinating, You're an overly skilled blogger.

    I've joined your rss feed and stay up for searching for extra of your wonderful post.

    Also, I've shared your site in my social networks

    Also visit my webpage - Mens Marines Ring

    ReplyDelete
  54. excellent issues altogether, you simply gained a new
    reader. What could you suggest about your submit that you just made some days in
    the past? Any sure?

    Here is my web page: Derwick Associates

    ReplyDelete
  55. Hey! Quick question that's entirely off topic.
    Do you know how to make your site mobile friendly? My site looks weird when
    viewing from my iphone4. I'm trying to find a theme or plugin
    that might be able to resolve this problem. If you have any suggestions, please share.

    Appreciate it!

    my web page; Edgard Romero Lazo

    ReplyDelete
  56. Hey there! This post couldn't be written any better!
    Reading through this post reminds me of my good old room mate!
    He always kept chatting about this. I will forward this article to
    him. Pretty sure he will have a good read. Many thanks for sharing!


    Feel free to surf to my webpage; Edgard Romero Lazo

    ReplyDelete
  57. Excellent post. I used to be checking continuously this weblog and I'm
    inspired! Extremely useful info particularly the final section :) I maintain such info a lot.
    I was looking for this certain info for a long time.

    Thanks and good luck.

    Feel free to visit my homepage :: Derwick Associates

    ReplyDelete
  58. Hi there! I know this is somewhat off-topic
    but I needed to ask. Does operating a well-established website such as yours
    require a large amount of work? I'm completely new
    to operating a blog but I do write in my diary everyday.
    I'd like to start a blog so I will be able to share my own experience and feelings online.
    Please let me know if you have any kind of suggestions
    or tips for new aspiring bloggers. Appreciate it!

    my webpage - Derwick Associates

    ReplyDelete
  59. It won't take much time to design a blog but writting content may take time.
    you can login to https://www.blogger.com with your gmail id and there you can create blog.

    but one thing i can say, if you use blogger you don't have access to your content i.e., you cannot migrate to other vendor with your data
    if you have migration plans in future, then go for wordpress.

    ReplyDelete
  60. Great post. I was checking continuously this blog and I'm
    inspired! Extremely helpful info particularly the remaining phase :) I maintain such info a lot.
    I used to be looking for this certain information
    for a long time. Thank you and best of luck.



    my site - Derwick Associates

    ReplyDelete
  61. Today, I went to the beachfront with my kids. I found a sea shell and gave
    it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She put the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is totally off
    topic but I had to tell someone!

    Look at my page - Leopoldo Alejandro Betancourt Lopez

    ReplyDelete
  62. Hello there! Quick question that's entirely off topic.
    Do you know how to make your site mobile friendly?
    My web site looks weird when viewing from my iphone4.
    I'm trying to find a theme or plugin that might be able to
    fix this issue. If you have any suggestions, please
    share. Thanks!

    My weblog :: Derwick Associates

    ReplyDelete
    Replies
    1. if your site is built on wordpress then you can choose responsive theme, which is compatible with all kinds of devices.

      Delete
  63. It's actually a great and useful piece of info.
    I'm satisfied that you just shared this helpful information
    with us. Please stay us up to date like this. Thank you for sharing.



    Reputation Management Press Releases

    ReplyDelete
  64. If you are going for best contents like I do, simply pay a visit this website all the time because it provides
    quality contents, thanks

    Press Release Reputation Management

    ReplyDelete
  65. After I initially left a comment I appear
    to have clicked the -Notify me when new comments are added-
    checkbox and from now on whenever a comment is added I recieve 4 emails with the same comment.
    Perhaps there is a way you are able to remove me from that service?
    Many thanks!

    Look into my blog ... Edgard Romero Lazo

    ReplyDelete
    Replies
    1. i don't think so..anyway you can spam it right :) ?

      Delete
  66. If some one wants to be updated with most recent technologies after
    that he must be pay a quick visit this web page
    and be up to date everyday.

    Brandon Hopkins

    ReplyDelete
  67. Heya i am for the first time here. I found this board and I find It truly useful & it helped
    me out much. I hope to give something back and aid others like you aided
    me.

    Also visit my web blog: edgard romero lazo

    ReplyDelete
  68. For most up-to-date information you have to pay a visit internet and
    on internet I found this web page as a best website for latest updates.


    Also visit my weblog; edgard romero lazo

    ReplyDelete
  69. This is my first time pay a visit at here and i am in fact happy to read everthing at alone place.


    Here is my page; Pedro Trebbau Lopez

    ReplyDelete
  70. I visited multiple websites however the audio feature for audio songs current at this website is in fact
    marvelous.

    Link Building for Reputation Management

    ReplyDelete
  71. Hola! I've been reading your site for some time now and
    finally got the bravery to go ahead and give you a shout out from Huffman Tx!
    Just wanted to mention keep up the excellent work!


    Pedro Trebbau Lopez

    ReplyDelete
  72. This excellent website definitely has all of the information
    I needed about this subject and didn't know who to ask.


    Pedro Trebbau Lopez

    ReplyDelete
  73. I must thank you for the efforts you have put in writing this website.
    I'm hoping to view the same high-grade blog posts from you later on as well.
    In fact, your creative writing abilities has inspired me to get my
    very own website now ;)

    Pedro Trebbau Lopez

    ReplyDelete
  74. My family members always say that I am killing my time here at net, except I know I
    am getting familiarity all the time by reading such good posts.



    Here is my webpage :: Derwick Associates

    ReplyDelete
  75. After checking out a number of the articles on your web page, I truly appreciate your way of writing
    a blog. I saved it to my bookmark site list and will be checking
    back soon. Please check out my web site as well
    and tell me your opinion.

    Here is my blog post Reputation Management

    ReplyDelete
  76. whoah this blog is wonderful i really like reading your posts.
    Keep up the good work! You know, many persons are hunting round
    for this info, you can aid them greatly.

    Here is my site :: Leopoldo Alejandro Betancourt Lopez

    ReplyDelete
  77. Hi! I realize this is somewhat off-topic however I had to ask.
    Does managing a well-established website like yours require a massive amount work?
    I'm completely new to operating a blog however I do write in
    my journal everyday. I'd like to start a blog so I can easily share my
    own experience and views online. Please let me know if you have any recommendations or tips for
    brand new aspiring bloggers. Thankyou!

    Feel free to surf to my web blog; Leopoldo Alejandro Betancourt Lopez

    ReplyDelete
  78. We're a group of volunteers and opening a brand new
    scheme in our community. Your web site provided us with helpful
    info to work on. You have done an impressive task and our entire group shall be thankful to you.



    Also visit my web site: jospeh sabeh jr

    ReplyDelete
  79. Thanks for your personal marvelous posting!
    I seriously enjoyed reading it, you could be a great
    author.I will ensure that I bookmark your blog and will often come back down the road.

    I want to encourage one to continue your great work, have a nice evening!



    Look at my web-site - Pedro Trebbau Lopez

    ReplyDelete
  80. Hi! I just wanted to ask if you ever have any issues with hackers?
    My last blog (wordpress) was hacked and I ended
    up losing a few months of hard work due to no backup.

    Do you have any methods to stop hackers?

    Feel free to visit my web page - Pedro Trebbau Lopez

    ReplyDelete
  81. i am using blogspot, you can see in the url. it is free and maintained by google.
    i didn't see any attacks till now.
    since you are wordpress user, it's better to follow some suggesstions like this
    http://wordpress.org/support/topic/plugin-wordfence-security-stopping-recurring-hacking-attempts

    ReplyDelete
  82. Wow, this paragraph is nice, my younger sister is analyzing such things, so I am going to tell her.


    Here is my web-site :: Jospeh Sabeh Jr

    ReplyDelete
  83. Fantastic beat ! I would like to apprentice att the same time as you amend your site, how could i subscribe for a blog
    site? The account aided me a appropriate deal. I have been a little bit familiar of this your
    broarcast provided shiny clear idea

    My website ... curtis debord

    ReplyDelete
  84. Usually I don't learn article on blogs, but I wish to say that this write-up very forced me to check out and do so!
    Your writing style has been amazed me. Thank you, very
    great post.

    curtis debord

    ReplyDelete
  85. I started blogging in my new site, where i will be posting regularly on various topics. you can subscribe if you like http://beacrazyprogrammer.com/

    ReplyDelete