Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #3682

Re: reversing a singly linked list

Newsgroups comp.programming
Date 2013-08-22 22:21 -0700
References <9dd27d03-1fd8-45d6-8958-03ab0f95c5e7@googlegroups.com> <32k519drkc944vpqf45nb34mbossvjmlgs@4ax.com>
Message-ID <fcac80b5-ebe6-4b7b-abbf-9e417fcd2112@googlegroups.com> (permalink)
Subject Re: reversing a singly linked list
From bob <bob@coolfone.comze.com>

Show all headers | View raw


On Monday, August 19, 2013 9:19:18 PM UTC-5, robert...@yahoo.com wrote:
> On Mon, 19 Aug 2013 17:57:20 -0700 (PDT), bob <bob@coolfone.comze.com>
> 
> wrote:
> 
> 
> 
> >Just out of curiosity, if someone asks you to write code to reverse a singly linked list at an interview, could you do it? (in C/C++)
> 
> >
> 
> >I struggled with this, and I'm not sure if it was a hard question or not.
> 
> 
> 
> 
> 
> Just run the list, and change all the link pointers to point to the
> 
> previously encountered item.  IOW:
> 
> 
> 
>   item *pp, *np, *tp;
> 
> 
> 
>   pp = NULL;
> 
>   np = headoflistpointer;
> 
>   while (np)
> 
>     {
> 
>     tp = np->next; 
> 
>     np->next = pp;
> 
>     pp = np;
> 
>     np = tp;
> 
>     }
> 
> 
> 
>   headoflistpointer = pp;
> 
> 
> 
> That assumes the task is to do the reverse in-place.

Yeah, there's a book on Amazon called

Elements of Programming Interviews: 300 Questions and Solutions

It pretty much has the solution you just gave with different variable names.

Thanks.

Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

reversing a singly linked list bob <bob@coolfone.comze.com> - 2013-08-19 17:57 -0700
  Re: reversing a singly linked list Ian Collins <ian-news@hotmail.com> - 2013-08-20 13:22 +1200
  Re: reversing a singly linked list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-08-20 02:26 +0100
  Re: reversing a singly linked list Robert Wessel <robertwessel2@yahoo.com> - 2013-08-19 21:19 -0500
    Re: reversing a singly linked list bob <bob@coolfone.comze.com> - 2013-08-22 22:21 -0700
  Re: reversing a singly linked list James Dow Allen <jdallen2000@yahoo.com> - 2013-08-22 04:11 -0700
    Re: reversing a singly linked list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-08-22 12:57 +0100
      Re: reversing a singly linked list James Dow Allen <jdallen2000@yahoo.com> - 2013-08-22 09:30 -0700

csiph-web