{"id":"09c6a6ed-4848-4462-a497-418d2075e552","name":"Reverse A Linkedlist","description":"You have given a pointer to the head node of a linked list, the task is to reverse the linked list. \r\nWe need to reverse the list by changing the links between nodes.","inputFormat":"1->2->3->4->5->6->7->null","outputFormat":"7->6->5->4->3->2->1->null","constraints":"0 &lt;= N &lt;= 10^6","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *reverse(ListNode *head)\r\n{\r\n\r\n return nullptr;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n\r\n ListNode *head = reverse(dummy->next);\r\n while (head != nullptr){\r\n cout << head->val << \" \";\r\n head = head->next;\r\n }\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode reverse(ListNode head) {\r\n \r\n return null;\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n ListNode head = reverse(dummy.next);\r\n while (head != null) {\r\n System.out.print(head.val + \" \");\r\nhead = head.next;\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n1 2 3 4 5 6 7","sampleOutput":"7 6 5 4 3 2 1","questionVideo":"https://www.youtube.com/embed/fEfLcFeVQBo","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"bb09a33f-d7dd-4ba6-95c3-68d7b7cbc6c6","name":"Reverse A Linkedlist","slug":"reverse-a-linkedlist","type":1}],"next":{"id":"1a1ffe76-c305-43e9-a81b-bb4da9e76fa7","name":"Reverse A Linkedlist MCQ","type":0,"slug":"reverse-a-linkedlist-mcq"},"prev":null}}

Reverse A Linkedlist

You have given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing the links between nodes.

{"id":"09c6a6ed-4848-4462-a497-418d2075e552","name":"Reverse A Linkedlist","description":"You have given a pointer to the head node of a linked list, the task is to reverse the linked list. \r\nWe need to reverse the list by changing the links between nodes.","inputFormat":"1->2->3->4->5->6->7->null","outputFormat":"7->6->5->4->3->2->1->null","constraints":"0 &lt;= N &lt;= 10^6","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *reverse(ListNode *head)\r\n{\r\n\r\n return nullptr;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n\r\n ListNode *head = reverse(dummy->next);\r\n while (head != nullptr){\r\n cout << head->val << \" \";\r\n head = head->next;\r\n }\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode reverse(ListNode head) {\r\n \r\n return null;\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n ListNode head = reverse(dummy.next);\r\n while (head != null) {\r\n System.out.print(head.val + \" \");\r\nhead = head.next;\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n1 2 3 4 5 6 7","sampleOutput":"7 6 5 4 3 2 1","questionVideo":"https://www.youtube.com/embed/fEfLcFeVQBo","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"bb09a33f-d7dd-4ba6-95c3-68d7b7cbc6c6","name":"Reverse A Linkedlist","slug":"reverse-a-linkedlist","type":1}],"next":{"id":"1a1ffe76-c305-43e9-a81b-bb4da9e76fa7","name":"Reverse A Linkedlist MCQ","type":0,"slug":"reverse-a-linkedlist-mcq"},"prev":null}}
plane

Editor


Loading...

Reverse A Linkedlist

easy

You have given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing the links between nodes.

Constraints

0 <= N <= 10^6

Format

Input

1->2->3->4->5->6->7->null

Output

7->6->5->4->3->2->1->null

Example

Sample Input

7 1 2 3 4 5 6 7

Sample Output

7 6 5 4 3 2 1

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode