Mercurial > blog
changeset 149:ed358312ab29
Restructured article. Improved code readability.
author | Oleksandr Gavenko <gavenkoa@gmail.com> |
---|---|
date | Tue, 23 Oct 2018 00:16:03 +0300 |
parents | cd67f687fc25 |
children | 0ba8bd17df48 |
files | ed00c7c3-a71e-4ed2-bfbb-ff2ac918960e/index.rst |
diffstat | 1 files changed, 22 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/ed00c7c3-a71e-4ed2-bfbb-ff2ac918960e/index.rst Mon Oct 22 23:58:05 2018 +0300 +++ b/ed00c7c3-a71e-4ed2-bfbb-ff2ac918960e/index.rst Tue Oct 23 00:16:03 2018 +0300 @@ -8,22 +8,33 @@ It is common interview programming task to reverse singly linked list in-place. -For example it mentioned here: +For example it is mentioned here: https://www.joelonsoftware.com/2006/10/25/the-guerrilla-guide-to-interviewing-version-30/ The Guerrilla Guide to Interviewing (version 3.0). -At interview with online editor I spent 40 min to complete task with many time spent debugging:: +During my interview I've spent around 40 min to complete task in online collaborative editor +spending most of the time on debugging:: reverse(list); print(list); -where I should write:: +which was off screen where I should write:: list = reverse(list); print(list); -Today I implemented it in 15 min:: +Another problems were that I forgot syntax how to declare self reference in C structure declaration, +below is wrong syntax:: + + typedef struct { + int val; + list_t *next; + } list_t; + +and header name for ``assert()``. + +Today I implemented task in 15 min:: #include <stdio.h> #include <stdlib.h> @@ -60,13 +71,13 @@ } list_t* list_reverse(list_t *head) { - list_t *tail = head; + list_t *rest = head; head = NULL; - while (tail != NULL) { - list_t *old_head = head; - head = tail; - tail = tail->next; - head->next = old_head; + while (rest != NULL) { + list_t *prev = head; + head = rest; + rest = rest->next; + head->next = prev; } return head; } @@ -80,20 +91,10 @@ return 0; } -To run I use:: +Run it with:: gcc -o rev.exe rev.c && ./rev.exe -The problem was that I forgot syntax how to declare self reference in structure declaration, below -is wrong syntax:: - - typedef struct { - int val; - list_t *next; - } list_t; - -and forgot header name for ``assert()``. - https://en.wikibooks.org/wiki/Data_Structures/Singly_Linked_Lists Singly Linked Lists.