DSA Sheet by Arsh (Linked List Easy Level Solution)
DSA Sheet by Arsh (Linked List Easy Level Solution)
Output: [3,4,5]
Output: true
ListNode*slow=head;
ListNode*fast=head;
while(fast!=NULL && fast->next!=NULL){
slow=slow->next;
fast=fast->next->next;
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
if(fast==slow){
return true;
}
}
return false;
}
3. Easy Level : Convert Binary Number in a Linked List to
Integer.
Code:
Output: 5
int num=head->val;
while(head->next!=NULL)
{
num=num*2+head->next->val;
head=head->next;
}
return num;
}
4. Easy Level : Remove Duplicates from Sorted List.
Code:
Input: head = [1,1,2]
Output: [1,2]
if(head==NULL)
return head;
ListNode* tmp=head;
while(tmp->next!=NULL)
{
if(tmp->next->val==tmp->next->val)
tmp->next=tmp->next->next;
SHIVANI PATEL 2
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
else
tmp=tmp->next;
}
return head;
}
5. Easy Level : Sort a linked list of 0s, 1s and 2s.
Code:
Input: 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> NULL
Output: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> NULL
Input: 1 -> 1 -> 2 -> 1 -> 0 -> NULL
Output: 0 -> 1 -> 1 -> 1 -> 2 -> NULL
Output: [1,2,3,4,5]
if(head==NULL)
return NULL;
head->next=removeElements(head->next,val);
if(head->val==val)
return head->next;
return head;
}
7. Easy Level : Merge Two Sorted Lists.
Code:
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
ListNode *ans=NULL;
if(!l1)
return l2;
else if(!l2)
return l1;
if(l1->val <= l2->val)
{
ans=l1;
ans->next=mergeTwoLists(l1->next,l2);
}
else
{
ans=l2;
ans->next=mergeTwoLists(l1,l2->next);
}
return ans;
}
8. Easy Level : Multiply two numbers represented by Linked Lists.
Code:
Input : 9->4->6
8->4
Output : 79464
SHIVANI PATEL 4
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
Input : 3->2->1
1->2
Output : 3852
if(l1){
num1 = ((num1)*10)%N + l1->data;
l1 = l1->next;
}
if(l2)
{
num2 = ((num2)*10)%N + l2->data;
l2 = l2->next;
}
}
return ((num1%N)*(num2%N))%N;
}
9. Easy Level : Intersection of Two Linked Lists.
Code:
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5],
skipA = 2, skipB = 3
SHIVANI PATEL 5
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
ListNode* a=headA;
ListNode* b=headB;
while(a!=b)
{
a = a == NULL? headB : a->next;
b = b == NULL ? headA : b->next;
}
return a;
}
10. Easy Level : Given only a pointer/reference to a node to be
deleted in a singly linked list, how do you delete it?
Code:
void deleteNode(Node* node)
{
Node* prev;
if(prev==NULL)
return;
else
{
while(node->next!=NULL)
{
node->data=node->next->data;
prev=node;
node=node->next;
}
prev->next=NULL;
}
}
11. Easy Level : Palindrome Linked List.
Code:
Input: head = [1,2,2,1]
Output: true
SHIVANI PATEL 6
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
}
if(fast!=NULL)
slow=slow->next;
while(!s.empty() and slow)
{
if(s.top()!=slow->val)
return false;
s.pop();
slow=slow->next;
}
return true;
}
12. Easy Level : Reverse Linked List.
Code:
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
SHIVANI PATEL 7