xxxxxxxxxx
def reverseLinkedList(self):
# Complexity Time: O(n), Complexity Memory: O(1)
# Initialize previous and current pointers
prev, curr = None, self.head
# loop over the linked list nodes until the current is Null
while curr:
# Store current's next node in a temporary variable
next = curr.next
# Reverse current node's pointer
curr.next = prev
# Move pointers one position ahead
prev = curr
curr = next
self.head = prev
xxxxxxxxxx
#include <iostream>
using namespace std;
typedef struct node Node;
struct node{
int data;
Node *next;
};
Node *Create_Node(int data, Node *next){
Node *new_node = (Node *) malloc(sizeof(Node));
new_node->data = data;
new_node->next = next;
return new_node;
}
Node *Insert_Node(Node *head, int data){
Node *new_node = Create_Node(data, NULL);
if(head == NULL){
return new_node;
}
Node *current_node = head;
while(current_node->next != NULL){
current_node = current_node->next;
}
current_node->next = new_node;
return head;
}
Node *Reverse_LinkedList(Node *head){
Node *temp = NULL;
Node *prev_data = NULL;
Node *current_node = head;
while(current_node != NULL){
prev_data = current_node;
current_node = current_node->next;
prev_data->next = temp;
temp = prev_data;
}
head = prev_data;
return head;
}
void Print_LinkedList(Node *head){
Node *current_node = head;
while(current_node != NULL){
printf("%d ",current_node->data);
current_node = current_node->next;
}
printf("\n");
}
int main() {
Node *head;
head = Insert_Node(head, 10);
head = Insert_Node(head, 20);
head = Insert_Node(head, 30);
Print_LinkedList(head);
head = Reverse_LinkedList(head);
Print_LinkedList(head);
return 0;
}
xxxxxxxxxx
public ListNode ReverseList(ListNode head)
{
if(head==null || head.next == null)
return head;
var t = ReverseList(head.next);
head.next.next = head;
head.next = null;
return t;
}
xxxxxxxxxx
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode current = head;
while(current != null) {
ListNode next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
}
xxxxxxxxxx
/*method to reverse a linked list */
reverse(list) {
let current = list.head;
let prev = null;
let next = null;
if (this.head) {
//only one node
if (!this.head.next) { return this; }
while (current) {
next = current.next;//store next node of current before change
current.next = prev;//change next of current by reverse the link
prev = current;//move prev node forward
current = next;//move current node forward
}
list.head = prev
return list
}
return "is empty"
}
xxxxxxxxxx
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode current = head;
ListNode prev = null;
ListNode next = null;
if(head == null)
{
return head;
}
while(current != null)
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
}
xxxxxxxxxx
/* Before changing next pointer of current node,
store the next node */
next = curr -> next
/* Change next pointer of current node */
/* Actual reversing */
curr -> next = prev
/* Move prev and curr one step ahead */
prev = curr
curr = next
xxxxxxxxxx
void reverse()
{
node* current, * prev, * temp;
current = head;
prev = NULL;
while (current != NULL)
{
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
head = prev;
}