We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Data Structures
  3. Linked Lists
  4. Get Node Value
  5. Discussions

Get Node Value

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 653 Discussions, By:

votes

Please Login in order to post a comment

  • salcio 6 years ago+ 0 comments

    Hi,

    I think I found quite nice soution - no recursion, no arrays. We iterate only once through whole list.

    int GetNode(Node *head,int positionFromTail)
    {
        int index = 0;
        Node* current = head;
        Node* result = head;
        while(current!=NULL)
        {
            current=current->next;
            if (index++>positionFromTail)
            {
                result=result->next;
            }
        }
        return result->data;
    }
    
    680|
    Permalink
  • RodneyShag 4 years ago+ 0 comments

    O(1) space complexity Java Iterative solution.

    From my HackerRank solutions.

    I use the "runner" technique. We make a runner pointer move k elements into the list. Then we create a curr pointer. We move both pointers 1 step at a time until runner is at the end. When this happens, curr will be at the kth to last element.

    Runtime: O(n)
    Space Complexity: O(1)

    int GetNode(Node head, int k) {
        Node curr   = head;
        Node runner = head;
        
        /* Move runner into the list by k elements */
        for (int i = 0; i < k; i++) {
            runner = runner.next;
        }
        
        /* Move both pointers */
        while (runner.next != null) {
            curr = curr.next;
            runner = runner.next;
        }
        
        return curr.data;
    }
    

    Let me know if you have any questions.

    70|
    Permalink
  • onuremreerol 4 years ago+ 0 comments

    Here is another clean and tidy solution in java. If you have any question, feel free to ask.

    int GetNode(Node head,int n) {
        Node temp = head;
        for (int i = 0; head.next != null; i++) {
            head = head.next;
            if ( i >= n) temp = temp.next;
        }
        return temp.data;            
    }
    
    12|
    Permalink
  • shubhamgoyal1101 4 years ago+ 0 comments

    python 3 code, easy to understand :). I used one pointer to get the length of the list than subtracted position from it and reach the required node via another pointer which was on head of the list.

    def GetNode(head, position):
        count=0
        second_head = head
        while head.next:
            head=head.next
            count+=1
        for i in range(count-position):
            second_head=second_head.next
        return second_head.data
    
    8|
    Permalink
  • __spartax 5 years ago+ 0 comments

    hii friends this is my solution

    def GetNode(head, position):
        stk = []
        t = head
        while t:
            stk = [t.data] + stk
            t = t.next
        return stk[position]
    
    6|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature