1// O(n) time & O(n) space
2function reverse(head) {
3 if (!head || !head.next) {
4 return head;
5 }
6 let tmp = reverse(head.next);
7 head.next.next = head;
8 head.next = undefined;
9 return tmp;
10}
11
1class LinkedList {
2
3 static Node head;
4
5 static class Node {
6
7 int data;
8 Node next;
9
10 Node(int d)
11 {
12 data = d;
13 next = null;
14 }
15 }
16
17 /* Function to reverse the linked list */
18 Node reverse(Node node)
19 {
20 Node prev = null;
21 Node current = node;
22 Node next = null;
23 while (current != null) {
24 next = current.next;
25 current.next = prev;
26 prev = current;
27 current = next;
28 }
29 node = prev;
30 return node;
31 }
32
33 // prints content of double linked list
34 void printList(Node node)
35 {
36 while (node != null) {
37 System.out.print(node.data + " ");
38 node = node.next;
39 }
40 }
41
42 public static void main(String[] args)
43 {
44 LinkedList list = new LinkedList();
45 list.head = new Node(85);
46 list.head.next = new Node(15);
47 list.head.next.next = new Node(4);
48 list.head.next.next.next = new Node(20);
49
50 System.out.println("Given Linked list");
51 list.printList(head);
52 head = list.reverse(head);
53 System.out.println("");
54 System.out.println("Reversed linked list ");
55 list.printList(head);
56 }
57}
1class recursion {
2 static Node head; // head of list
3 static class Node {
4 int data;
5 Node next;
6 Node(int d)
7 { data = d;
8 next = null; } }
9 static Node reverse(Node head)
10 {
11 if (head == null || head.next == null)
12 return head;
13 /* reverse the rest list and put the first element
14 at the end */
15 Node rest = reverse(head.next);
16 head.next.next = head;
17 /* tricky step -- see the diagram */
18 head.next = null;
19 /* fix the head pointer */
20 return rest;
21 } /* Function to print linked list */
22 static void print()
23 {
24 Node temp = head;
25 while (temp != null) {
26 System.out.print(temp.data + " ");
27 temp = temp.next;
28 }
29 System.out.println();
30 }
31 static void push(int data)
32 {
33 Node temp = new Node(data);
34 temp.next = head;
35 head = temp;
36 } /* Driver program to test above function*/
37public static void main(String args[])
38{
39 /* Start with the empty list */
40 push(20);
41 push(4);
42 push(15);
43 push(85);
44 System.out.println("Given linked list");
45 print();
46 head = reverse(head);
47 System.out.println("Reversed Linked list");
48 print();
49} } // This code is contributed by Prakhar Agarwal
1 /* Before changing next pointer of current node,
2 store the next node */
3 next = curr -> next
4 /* Change next pointer of current node */
5 /* Actual reversing */
6 curr -> next = prev
7 /* Move prev and curr one step ahead */
8 prev = curr
9 curr = next
10
1#include<bits/stdc++.h>
2
3using namespace std;
4
5struct node {
6 int data;
7 struct node *next;
8};
9
10// To create a demo we have to construct a linked list and this
11// function is to push the elements to the list.
12void push(struct node **head_ref, int data) {
13 struct node *node;
14 node = (struct node*)malloc(sizeof(struct node));
15 node->data = data;
16 node->next = (*head_ref);
17 (*head_ref) = node;
18}
19
20// Function to reverse the list
21void reverse(struct node **head_ref) {
22 struct node *temp = NULL;
23 struct node *prev = NULL;
24 struct node *current = (*head_ref);
25 while(current != NULL) {
26 temp = current->next;
27 current->next = prev;
28 prev = current;
29 current = temp;
30 }
31 (*head_ref) = prev;
32}
33
34// To check our program
35void printnodes(struct node *head) {
36 while(head != NULL) {
37 cout<<head->data<<" ";
38 head = head->next;
39 }
40}
41
42// Driver function
43int main() {
44 struct node *head = NULL;
45 push(&head, 0);
46 push(&head, 1);
47 push(&head, 8);
48 push(&head, 0);
49 push(&head, 4);
50 push(&head, 10);
51 cout << "Linked List Before Reversing" << endl;
52 printnodes(head);
53 reverse(&head);
54 cout << endl;
55 cout << "Linked List After Reversing"<<endl;
56 printnodes(head);
57 return 0;
58}
59
1// using iterative method to reverse linked list in JavaScript
2// time complexity: O(n) & space complexity: O(1)
3reverse() {
4 if (!this.head.next) {
5 return this.head;
6 }
7
8 let prevNode = null;
9 let currNode = this.head;
10 let nextNode = this.head;
11 while(nextNode){
12 nextNode = currNode.next;
13 currNode.next = prevNode;
14 prevNode = currNode;
15 currNode = nextNode;
16 }
17 this.head = prevNode;
18 return this.printList();
19 }