Take the 2-minute tour ×

So this actually reverses my list, but for some reason my tail->next gets set to 0 instead of NULL. This causes my print function to get stuck in an infinite loop.

struct ListNode { int value; struct ListNode* next; struct ListNode* previous; };

void reverseList(struct ListNode** head,struct ListNode** tail)
struct ListNode* current = *head;
struct ListNode* temp;
while(current != NULL)
    temp = current->previous;
    current->previous = current->next;
    current->next = temp;
    current = current->previous;
temp = *head;
*head = *tail;
*tail = temp;

void printList(struct ListNode* head)
while(head != NULL)
    printf("%d <--> ",head->value);
    head = head->next;

int main()
int i = 0;
struct ListNode* head = malloc(sizeof(struct ListNode));
struct ListNode* tail = malloc(sizeof(struct ListNode));
head->value = 1;
head->previous = NULL;
head->next = NULL;
tail = head;
while(i < 5)
share|improve this question
Did you try using a debugger to see why this is happening? –  user93353 Apr 17 '13 at 3:47
why do you create two temps ?, temp->next = prev; is exactly wrong at first iteration –  Grijesh Chauhan Apr 17 '13 at 3:50
the commented code is what im asking about. The non-commented code works. The temp is local to the while loop if I'm not mistaken. –  user2285010 Apr 17 '13 at 3:54
Could you post the definition of your ListNode struct? –  2to1mux Apr 17 '13 at 4:17
If you post the exact code you are using leaving the commented code out it would help. Its not clear what assignments you are making and which ones you are skipping before the commented code –  gap_j Apr 17 '13 at 4:19

Your Answer



Browse other questions tagged or ask your own question.