1/*
2public class ListNode {
3    public int val;
4    public ListNode next;
5    public ListNode(int x) { val = x; next = null; }
6}
7*/
8
9public static ListNode[] reverse_linked_list(ListNode head) {
10
11        ListNode prev = null;
12        ListNode current = head;
13        ListNode next;
14
15        ListNode tail = head;
16
17        while (current != null) {
18
19            next = current.next;
20            current.next = prev;
21            prev = current;
22            current = next;
23        }
24
25        head = prev;
26
27        ListNode[] result = {head, tail};
28
29        return result;
30}1//Iterative program in C++ to reverse a linked list in groups of k
2
3//	(just before end of 2nd iteration of outer loop) [ 1->2->3->4->5->NULL ]
4// 1 <- 2    3 <- 4  5 ->NULL   (k=2)
5   ^		 ^		 ^
6   |		 |		 |
7prev_tail temp_head walker
8
9void reverse_by_k(Node **head,int k){
10    Node* temp_head = *head,*walker = *head,*prev_tail = NULL;
11    while(walker){
12        int i=0;
13        Node *temp = NULL,*prev = NULL;
14        //initialize temporary head to set previous tail later
15        temp_head = walker;
16        
17        //reverse group of k nodes
18        while(i<k && walker){
19            temp = walker->next;
20            walker->next = prev;
21            prev = walker;
22            walker = temp;
23            i++;
24        }
25        
26        if(prev_tail){
27            //previous tail has to point to temporary head of current group
28            prev_tail->next = prev;
29            prev_tail = temp_head;
30        } else{
31            prev_tail = *head;
32            *head = prev;
33        }
34    }
35}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
101#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}
591#include <stdio.h>
2struct Node {
3   int data;
4   struct Node* next;
5   Node(int data){
6      this->data = data;
7      next = NULL;
8   }
9};
10struct LinkedList {
11   Node* head;
12   LinkedList(){
13      head = NULL;
14   }
15   void interReverseLL(){
16      Node* current = head;
17      Node *prev = NULL, *after = NULL;
18      while (current != NULL) {
19         after = current->next;
20         current->next = prev;
21         prev = current;
22         current = after;
23      }
24      head = prev;
25   }
26   void print() {
27      struct Node* temp = head;
28      while (temp != NULL) {
29         printf("%d ", temp-> data);
30         temp = temp->next;
31      }
32      printf("\n");
33   }
34   void push(int data){
35      Node* temp = new Node(data);
36      temp->next = head;
37      head = temp;
38   }
39};
40int main() {
41   LinkedList linkedlist;
42   linkedlist.push(85);
43   linkedlist.push(10);
44   linkedlist.push(65);
45   linkedlist.push(32);
46   linkedlist.push(9);
47   printf("Linked List : \t");
48   linkedlist.print();
49   linkedlist.interReverseLL();
50   printf("Reverse Linked List : \t");
51   linkedlist.print();
52   return 0;
53}
54
55
56Output
57Linked List : 9 32 65 10 85
58Reverse Linked List : 85 10 65 32 9