π 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)