๐ Today's Learning:
๐ DSA
- Check if a linked list is sorted or not
- Removing duplicate elements from a sorted linked list
๐ Dev
- states in React
๐ Some Key Highlights:
DSA
- Check if a linked list is sorted or not
โ make a variable x, initialized with int min. Iterate on the linked list using a pointer p. at each pass store the value (pโval) in x and also at each pass check if pโval < x. If yes then the list is not sorted so return false. otherwise return true. This is how we are comparing current value with previous value (stored in x). Note that for first iteration it compares with INT_MIN which will always be the minimum value so it wonโt return false here. T.C. O(n) | S.C. O(1)
- Removing duplicate elements from a sorted linked list
โ Do it using two pointers. start from p = head and q = pโnext. Till the time pโval! = qโval keep on sliding q and p ahead (p=q; q=qโnext;). If qโval = pโval then we have to delete one. Here visulaize by drawing three nodes. q is on middle node and p on first node. Now we have to make pโ next = qโ next. Then delete q (this would free the middle node from memory) Now do q = pโnext. Overall now p has the third node as the next node, and q is on the third node. So the middle one, which was a duplicate is deleted. T.C. O(n) | S.C O(1)
DEV
The state in React is an instance of the React Component Class that can be defined as an object of a set of observable properties that control the behavior of the component. In other words, the State of a component is an object that holds some information that may change over the lifetime of the component.
#100daysofcode #1percentplusplus #coding #dsa
Top comments (0)