Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8624675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:35:24+00:00 2026-06-12T07:35:24+00:00

In a job interview I was asked to write a function in C that

  • 0

In a job interview I was asked to write a function in C that recursively reverses a linked list, returns the new first node, and doesn’t use any new nodes.

How can you do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:35:26+00:00Added an answer on June 12, 2026 at 7:35 am

    Here’s the general idea, in an agnostic language with C-like syntax:

    Node invert(Node list, Node acc) {
        if (list == null)
            return acc;
        Node next = list.next;
        list.next = acc;
        return invert(next, list);
    }
    

    The above function receives the first node of the list to be inverted and the accumulated value so far, and returns the head of the newly-inverted list – the reversal of nodes is done in-place, no extra space is allocated (besides a local variable in the stack). Call it like this:

    invert(firstNodeOfList, null);
    

    This is an example of a tail recursion: the result gets collected in the acc parameter, and when each recursive call returns, there’s nothing left to do, just return the accumulated value.

    UPDATE:

    In a functional language it’s easier and more natural to write a function to reverse a list without using a local variable, for instance look at the following code in Scheme – it has drawback, that it will create a new list node when calling the cons procedure:

    (define (invert lst acc)
      (if (empty? lst)
          acc
          (invert (rest lst)
                  (cons (first lst) acc))))
    
    (invert '(1 2 3 4 5) '())
    > '(5 4 3 2 1)
    

    Bottom line: you either create a new node or create a new local variable at each recursive call, but unless the programming language offers an expression for sequential execution and the compiler guarantees evaluation order (see @WillNess’ answer) you can’t eliminate both and have a working algorithm. Better play it safe and use a temporary variable for enforcing evaluation order and always obtaining correct results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A friend of mine was asked, during a job interview, to write a program
On my first job interview, I was asked why did I build my own
I have been asked recently in a job interview to develop an algorithm that
This is a question that I was asked on a job interview some time
This task has already been asked/answered, but I recently had a job interview that
I was asked this question in a job interview recently. I answered that I
I was recently asked in a job interview to resolve a programming puzzle that
I was asked this question in a job interview, and I'd like to know
I went to a PHP job interview, I was asked to implement a piece
I was asked this question recently during my job interview, and I couldn't answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.