• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
January 23, 2024 |630 Views
SDE Sheet - Given a Linked list of 0s, 1s and 2s, sort it
  Share  1 Like
Description
Discussion

This video is part of Linked List section under GFG SDE Sheet.

In this video, we are given a linked list of N nodes where nodes can contain values 0s, 1s, and 2s only. The task is to segregate 0s, 1s, and 2s linked list such that all zeros segregate to head side, 2s at the end of the linked list, and 1s in the mid of 0s and 2s.

Example :

Input:
N = 8
value[] = {1,2,2,1,2,0,2,2}
Output: 0 1 1 2 2 2 2 2

Explanation: All the 0s are segregated to the left end of the linked list, 2s to the right end of the list, and 1s in between.

Do check out:-

Article: https://www.geeksforgeeks.org/sort-a-Linked-list-of-0s-1s-or-2s/
Problem: https://practice.geeksforgeeks.org/problems/given-a-linked-list-of-0s-1s-and-2s-sort-it/1
SDE Sheet: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More