Posts

Leetcode Solution - Remove Duplicates from Sorted List II

  Que:-   Given the  head  of a sorted linked list,  delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list . Return  the linked list  sorted  as well . Link of Question :-  https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/ Solution:- #Complexity:- Time Complexity = O(N) Space Complexity = O(1) #Approach:- Here I have used the sentinel node. Sentinel nodes are specially designed nodes that do not hold or refer to any data of the Singly LinkedList. You can learn more about Sentinel node from here:- https://www.geeksforgeeks.org/doubly-linked-list-using-sentinel-nodes/ #Code:- /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode() : val(0), next(nullptr) {}  *     ListNode(int x) : val(x), next(nullptr) {}  *     ListNode(i...

Linked List Cycle II - Leetcode Solution

  Que:- Given the   head   of a linked list, return   the node where the cycle begins. If there is no cycle, return  null . There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the  next  pointer. Internally,  pos  is used to denote the index of the node that tail's  next  pointer is connected to ( 0-indexed ). It is  -1  if there is no cycle.  Note that   pos   is not passed as a parameter . Do not modify  the linked list. Approach Floyd Cycle algorithm is used in this Solution Complexity Time complexity:- O(N-L),where N is the number of nodes in Linked List and L is the length of loop Space complexity:- O(1) Code /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public : ListNode * detectCycle ( ListNod...