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}
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}
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
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//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}
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 }