---
# System prepended metadata

title: Reverse a linked list-iterative method

---

[TOC]

![](https://hackmd.io/_uploads/HJpSpbdO2.png)


## 實現想法

![](https://hackmd.io/_uploads/BJm4ab__3.png)

透過 privious , current(以前tempt 概念) , next(紀錄下個要走的節點) 


```cpp
void reverseLinkList(){

    struct node *privious,*current,*next;
    current = head;
    privious = NULL;

    //walk
    while(current != NULL){

        next = current->next; //record 下一個hop 
        current->next = privious; //reverse pointer 方向
        privious=current; //留給下個node 交換用
        current= next;// move to the next node 

    }
    head = privious; //update head pointer 

}
```
or
```cpp
struct node* reverseLinkList(struct node *head){
    struct node *privious,*current,*next;
    current = head;
    privious = NULL;

    //walk
    while(current != NULL){

        next = current->next; //record 下一個hop 
        current->next = privious; //reverse pointer 方向
        privious=current; //留給下個node 交換用
        current= next;// move to the next node 

    }
    head = privious; //update head pointer 
    return head;
}
```

![](https://hackmd.io/_uploads/H1g8fMdOn.png)




Reseult and code 


```cpp
#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node* next;
};

struct node* insertF(int data, struct node* head) {
    //allocate memory (node address)
    struct node* temp = (struct node*)malloc(sizeof(struct node));

    temp->data = data; //(node data)
    temp->next = head; //(node pointer)

    head = temp;
    return head;
}

void printF(struct node* head) {
    struct node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

struct node* reverseLinkList(struct node* head) {
    struct node* previous = NULL;
    struct node* current = head;
    struct node* next = NULL;

    while (current != NULL) {
        next = current->next; //record 下一個hop 
        current->next = previous; //reverse pointer 方向
        previous=current; //留給下個node 交換用
        current= next;// move to the next node 
    }

    head = previous;
    return head;
}

int main() {
    struct node* head = NULL;

    head = insertF(3, head);
    head = insertF(4, head);
    head = insertF(5, head);
    head = insertF(6, head);

    printf("Original Link List: ");
    printF(head);

    printf("Reverse Link List: ");
    head = reverseLinkList(head);
    printF(head);

    return 0;
}

```

```
Original Link List: 6 5 4 3 
Reverse Link List: 3 4 5 6 
```