{"id":"b2f276a1-938f-45d7-aa8b-96a5df416aa7","name":"Segregate Node Of Linkedlist Over Last Index.","description":"1. Given a singly linklist, Segregate Node of LinkedList over lastindex and return pivot node of linkedlist.\r\n2. pivot is always be last index of linkedlist.\r\n3. After segregation pivot Element should have to be present at correct position as in sorted linkedlist.\r\n","inputFormat":"1->5->2->9->5->14->11->1->10->10->1->3->null\r\n","outputFormat":"3->5->9->5->14->11->10->10->null \r\n","constraints":"-10^6 &lt;= size Of LinkedList &lt;= 10^6\r\n","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 *segregateOnLastIndex(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode *createList(int n)\r\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 return dummy->next\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *h1 = createList(n);\r\n h1 = segregateOnLastIndex(h1);\r\n printList(h1);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\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 segregateOnLastIndex(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\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 return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n h1 = segregateOnLastIndex(h1);\r\n printList(h1);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"12\r\n1 5 2 9 5 14 11 1 10 10 1 3 \r\n","sampleOutput":"3 5 9 5 14 11 10 10 ","questionVideo":"https://www.youtube.com/embed/R9ak6JgWMAw","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":"0465fd3b-1ce6-4981-8358-bbf32ed4a398","name":"Segregate Node Of Linkedlist Over Last Index.","slug":"segregate-node-of-linkedlist-over-last-index","type":1}],"next":{"id":"009f1856-0e8c-425e-b752-bb981d5371e4","name":"Segregate Node Of Linkedlist Over Pivot Index","type":1,"slug":"segregate-node-of-linkedlist-over-pivot-index"},"prev":{"id":"808aadd3-2f71-48fd-a101-c925aa9beff6","name":"Segregate 012 Node Of Linkedlist Over Swapping Nodes","type":3,"slug":"segregate-012-node-of-linkedlist-over-swapping-nodes"}}}

Segregate Node Of Linkedlist Over Last Index.

1. Given a singly linklist, Segregate Node of LinkedList over lastindex and return pivot node of linkedlist. 2. pivot is always be last index of linkedlist. 3. After segregation pivot Element should have to be present at correct position as in sorted linkedlist.

{"id":"b2f276a1-938f-45d7-aa8b-96a5df416aa7","name":"Segregate Node Of Linkedlist Over Last Index.","description":"1. Given a singly linklist, Segregate Node of LinkedList over lastindex and return pivot node of linkedlist.\r\n2. pivot is always be last index of linkedlist.\r\n3. After segregation pivot Element should have to be present at correct position as in sorted linkedlist.\r\n","inputFormat":"1->5->2->9->5->14->11->1->10->10->1->3->null\r\n","outputFormat":"3->5->9->5->14->11->10->10->null \r\n","constraints":"-10^6 &lt;= size Of LinkedList &lt;= 10^6\r\n","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 *segregateOnLastIndex(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode *createList(int n)\r\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 return dummy->next\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *h1 = createList(n);\r\n h1 = segregateOnLastIndex(h1);\r\n printList(h1);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\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 segregateOnLastIndex(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\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 return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n h1 = segregateOnLastIndex(h1);\r\n printList(h1);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"12\r\n1 5 2 9 5 14 11 1 10 10 1 3 \r\n","sampleOutput":"3 5 9 5 14 11 10 10 ","questionVideo":"https://www.youtube.com/embed/R9ak6JgWMAw","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":"0465fd3b-1ce6-4981-8358-bbf32ed4a398","name":"Segregate Node Of Linkedlist Over Last Index.","slug":"segregate-node-of-linkedlist-over-last-index","type":1}],"next":{"id":"009f1856-0e8c-425e-b752-bb981d5371e4","name":"Segregate Node Of Linkedlist Over Pivot Index","type":1,"slug":"segregate-node-of-linkedlist-over-pivot-index"},"prev":{"id":"808aadd3-2f71-48fd-a101-c925aa9beff6","name":"Segregate 012 Node Of Linkedlist Over Swapping Nodes","type":3,"slug":"segregate-012-node-of-linkedlist-over-swapping-nodes"}}}
plane

Editor


Loading...

Segregate Node Of Linkedlist Over Last Index.

easy

1. Given a singly linklist, Segregate Node of LinkedList over lastindex and return pivot node of linkedlist. 2. pivot is always be last index of linkedlist. 3. After segregation pivot Element should have to be present at correct position as in sorted linkedlist.

Constraints

-10^6 <= size Of LinkedList <= 10^6

Format

Input

1->5->2->9->5->14->11->1->10->10->1->3->null

Output

3->5->9->5->14->11->10->10->null

Example

Sample Input

12 1 5 2 9 5 14 11 1 10 10 1 3

Sample Output

3 5 9 5 14 11 10 10

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode