# Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)

Examples:

```Input : Head of following linked list
1->2->3->4->NULL
Output : Linked list should be changed to,
4->3->2->1->NULL

1->2->3->4->5->NULL
Output : Linked list should be changed to,
5->4->3->2->1->NULL
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We have seen how to reverse a linked list in article Reverse a linked list. In iterative method we had used 3 pointers prev, cur and next. Below is an interesting approach that uses only two pointers. The idea is to use XOR to swap pointers.

## C/C++

```// C++ program to reverse a linked list using two pointers.
#include<bits/stdc++.h>
using namespace std;
typedef uintptr_t ut;

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

/* Function to reverse the linked list using 2 pointers */
{
struct Node* prev   = NULL;

// at last prev points to new head
while (current != NULL)
{
// This expression evaluates from left to right
// current->next = prev, changes the link fron
//                 next to prev node
// prev = current, moves prev to current node for
//        next reversal of node
// This example of list will clear it more 1->2->3->4
// initially prev = 1, current = 2
// Final expression will be current = 1^2^3^2^1,
// as we know that bitwise XOR of two same
// numbers will always be 0 i.e; 1^1 = 2^2 = 0
// After the evaluation of expression current = 3 that
// means it has been moved by one node from its
// previous position
current = (struct Node *) ((ut)prev^(ut)current^(ut)(current->next)^
(ut)(current->next = prev)^
(ut)(prev = current));
}

}

/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));

/* put in the data  */
new_node->data  = new_data;

/* link the old list off the new node */

/* move the head to point to the new node */
}

/* Function to print linked list */
{
while (temp != NULL)
{
printf("%d  ", temp->data);
temp = temp->next;
}
}

/* Driver program to test above function*/
int main()
{

return 0;
}
```

## Python

```# Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
# Python program to reverse a linked list
# node class
class node:

# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

# Function to reverse the linked list
def reverse(self):
prev = None
# Described here http://www.geeksforgeeks.org/
# how-to-swap-two-variables-in-one-line/
while(current is not None):
# This expression evaluates from left to right
# current->next = prev, changes the link fron
#                 next to prev node
# prev = current, moves prev to current node for
#        next reversal of node
# This example of list will clear it more 1->2
# initially prev = 1, current = 2
# Final expression will be current = 1, prev = 2
next, current.next = current.next, prev
prev, current = current, next

# Function to push a new node
def push(self, new_data):
# allocate node and put in the data
new_node = node(new_data)
# link the old list off the new node
# move the head to point to the new node

# Function to print the linked list
def printList(self):
while(temp):
print temp.data,
temp = temp.next

# Driver program to test above functions
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)

llist.printList()
llist.reverse()
llist.printList()

#This code is contributed by Afzal Ansari
```

Output:

```Given linked list
85 15 4 20
20 4 15 85
```

Time Complexity: O(n)
Space Complexity: O(1)

This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.4 Average Difficulty : 3.4/5.0
Based on 25 vote(s)