Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

1.

Function to check if a singly linked list is palindrome

bool isPalin(Node* head){

Node* slow= head;

stack <int> s;

while(slow != NULL){
s.push(slow->data);
slow = slow->ptr;
}

while(head != NULL ){
int i=s.top();
s.pop();
if(head -> data != i){
return false;
}
head=head->ptr;
}
return true;
}

2. Remove duplicates from a sorted linked list

void removeDuplicates(Node* head)


{
current = head;

next_next;

if (current == NULL)
return;

while (current->next != NULL)


{
if (current->data == current->next->data)
{
next_next = current->next->next;
free(current->next);
current->next = next_next;
}
else /* This is tricky: only advance if no deletion */
{
current = current->next;
}
}
}

void removeDuplicates(Node* head)


{
Node* to_free;

if (head == NULL)
return;
if (head->next != NULL)
{
if (head->data == head->next->data)
{
to_free = head->next;
head->next = head->next->next;
free(to_free);
removeDuplicates(head);
}
else
{
removeDuplicates(head->next);
}
}
}

Node* removeDuplicates(Node* head)


{
Node *temp = head, *prev = head;
while (temp != NULL) {
if (temp->data != prev->data) {
prev->next = temp;
prev = temp;
}
temp = temp->next;
}
if (prev != temp)
prev->next = NULL;
return head;
}

3. Remove duplicates from an unsorted linked list

void removeDuplicates(struct Node* start)


{
struct Node *ptr1, *ptr2, *dup;
ptr1 = start;
while (ptr1 != NULL && ptr1->next != NULL) {
ptr2 = ptr1;
while (ptr2->next != NULL) {
if (ptr1->data == ptr2->next->data) {
dup = ptr2->next;
ptr2->next = ptr2->next->next;
delete (dup);
}
else /* This is tricky */
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
}

4. Swap nodes in a linked list without swapping data

void swapNodes(Node** head_ref, int x, int y)


{
if (x == y)
return;
Node *prevX = NULL, *currX = *head_ref;
while (currX && currX->data != x) {
prevX = currX;
currX = currX->next;
}
Node *prevY = NULL, *currY = *head_ref;
while (currY && currY->data != y) {
prevY = currY;
currY = currY->next;
}
if (currX == NULL || currY == NULL)
return;
if (prevX != NULL)
prevX->next = currY;
else // Else make y as new head
*head_ref = currY;
if (prevY != NULL)
prevY->next = currX;
else
*head_ref = currX;
Node* temp = currY->next;
currY->next = currX->next;
currX->next = temp;
}

void swapNodes(Node** head_ref, int x, int y)


{
if (x == y)
return;
Node **a = NULL, **b = NULL;
while (*head_ref) {

if ((*head_ref)->data == x) {
a = head_ref;
}

else if ((*head_ref)->data == y) {
b = head_ref;
}
head_ref = &((*head_ref)->next);
}
if (a && b) {
swap(*a, *b);
swap(((*a)->next), ((*b)->next));
}
}

5. Pairwise swap elements of a given linked list

void pairWiseSwap(Node* head)


{
Node* temp = head;
while (temp != NULL && temp->next != NULL) {
swap(temp->data,
temp->next->data);
temp = temp->next->next;
}
}

void pairWiseSwap(struct node* head)


{
if (head != NULL && head->next != NULL) {
swap(head->data, head->next->data);
pairWiseSwap(head->next->next);
}
}

6. Move last element to front of a given Linked List

void moveToFront(Node **head_ref)


{
if (*head_ref == NULL || (*head_ref)->next == NULL)
return;
Node *secLast = NULL;
Node *last = *head_ref;
while (last->next != NULL)
{
secLast = last;
last = last->next;
}
secLast->next = NULL;
last->next = *head_ref;
*head_ref = last;
}

7. Intersection of two Sorted Linked Lists

void push(Node** head_ref, int new_data);


Node* sortedIntersect(Node* a, Node* b)
{
Node dummy;
Node* tail = &dummy;
dummy.next = NULL;
while (a != NULL && b != NULL) {
if (a->data == b->data) {
push((&tail->next), a->data);
tail = tail->next;
a = a->next;
b = b->next;
}
else if (a->data < b->data)
a = a->next;
else
b = b->next;
}
return (dummy.next);
}

8. Intersection point of two Linked Lists.

int _getIntesectionNode(int d, Node* head1, Node* head2);


int getIntesectionNode(Node* head1, Node* head2)
{
int c1 = getCount(head1);
int c2 = getCount(head2);
int d;
if (c1 > c2) {
d = c1 - c2;
return _getIntesectionNode(d, head1, head2);
}
else {
d = c2 - c1;
return _getIntesectionNode(d, head2, head1);
}
}

9. Segregate even and odd nodes in a Linked List

void segregateEvenOdd(Node **head_ref)


{
Node *end = *head_ref;
Node *prev = NULL;
Node *curr = *head_ref;
while (end->next != NULL)
end = end->next;

Node *new_end = end;


while (curr->data % 2 != 0 && curr != end)
{
new_end->next = curr;
curr = curr->next;
new_end->next->next = NULL;
new_end = new_end->next;
}
if (curr->data%2 == 0)
{
*head_ref = curr;
while (curr != end)
{
if ( (curr->data) % 2 == 0 )
{
prev = curr;
curr = curr->next;
}
else
{
prev->next = curr->next;
curr->next = NULL;
new_end->next = curr;
new_end = curr;
curr = prev->next;
}
}
}

else prev = curr;


if (new_end != end && (end->data) % 2 != 0)
{
prev->next = end->next;
end->next = NULL;
new_end->next = end;
}
return;
}

10. Reverse a linked list

void reverse()
{
Node* current = head;
Node *prev = NULL, *next = NULL;

while (current != NULL) {


next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
Node* reverse(Node* head)
{
if (head == NULL || head->next == NULL)
return head;
Node* rest = reverse(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}

void reverseLL(Node** head)


{
stack<Node*> s;
Node* temp = *head;
while (temp->next != NULL) {
s.push(temp);
temp = temp->next;
}
*head = temp;
while (!s.empty()) {
temp->next = s.top();
s.pop();
temp = temp->next;
}
temp->next = NULL;
}

11. Recursive function to print reverse of a Linked List

void printReverse(Node* head)


{
if (head == NULL)
return;
printReverse(head->next);
cout << head->data << " ";
}

12. Merge two sorted linked lists such that merged list is
in reverse order

Node* SortedMerge(Node *a, Node *b)


{
if (a==NULL && b==NULL) return NULL;
Node *res = NULL;
while (a != NULL && b != NULL)
{
if (a->key <= b->key)
{
Node *temp = a->next;
a->next = res;
res = a;
a = temp;
}
else
{
Node *temp = b->next;
b->next = res;
res = b;
b = temp;
}
}
while (a != NULL)
{
Node *temp = a->next;
a->next = res;
res = a;
a = temp;
}
while (b != NULL)
{
Node *temp = b->next;
b->next = res;
res = b;
b = temp;
}
return res;
}

13. Reverse a Linked List in groups of given size

Node* reverse(Node* head, int k)


{
if (!head)
return NULL;
Node* current = head;
Node* next = NULL;
Node* prev = NULL;
int count = 0;
while (current != NULL && count < k) {
next = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}
if (next != NULL)
head->next = reverse(next, k);
return prev;
}
struct Node* Reverse(struct Node* head, int k)
{
stack<Node*> mystack;
struct Node* current = head;
struct Node* prev = NULL;
while (current != NULL) {
int count = 0;
while (current != NULL && count < k) {
mystack.push(current);
current = current->next;
count++;
}
while (mystack.size() > 0) {
if (prev == NULL) {
prev = mystack.top();
head = prev;
mystack.pop();
} else {
prev->next = mystack.top();
prev = prev->next;
mystack.pop();
}
}
}
prev->next = NULL;

return head;
}

14.Alternate Odd and Even Nodes in a Singly Linked List

void rearrangeOddEven(Node* head)


{
stack<Node*> odd;
stack<Node*> even;
int i = 1;

while (head != nullptr) {


if (head->data % 2 != 0 && i % 2 == 0) {
odd.push(head);
}

else if (head->data % 2 == 0 && i % 2 != 0) {


even.push(head);
}
head = head->next;
i++;
}
while (!odd.empty() && !even.empty()) {
swap(odd.top()->data, even.top()->data);
odd.pop();
even.pop();
}
}

15. Delete alternate nodes of a Linked List

void deleteAlt(Node *head)


{
if (head == NULL)
return;
Node *prev = head;
Node *node = head->next;

while (prev != NULL && node != NULL)


{
prev->next = node->next;
prev = prev->next;
if (prev != NULL)
node = prev->next;
}
}

void deleteAlt(Node *head)


{
if (head == NULL)
return;
Node *node = head->next;
if (node == NULL)
return;
head->next = node->next;
free(node);
deleteAlt(head->next);
}

16. Identical Linked Lists

bool areIdentical(struct Node *a,


struct Node *b)
{
while (a != NULL && b != NULL)
{
if (a->data != b->data)
return false;
a = a->next;
b = b->next;
}
return (a == NULL && b == NULL);
}

bool areIdentical(Node *a, Node *b)


{
if (a == NULL && b == NULL)
return true;
if (a != NULL && b != NULL)
return (a->data == b->data) &&
areIdentical(a->next, b->next);
return false;
}

17. Delete nodes which have a greater value on right side

deleteNodesOnRightSide(Node head){
if(head == null || head.next == null) return head;
Node nextNode = deleteNodesOnRightSide(head.next);
if(nextNode.data > head.data) return nextNode;
head.next = nextNode;
return head;
}

18. Add two numbers represented by linked lists | Set 1

Node* addTwoNumbers(Node* l1, Node* l2)


{
Node* prev = NULL;
stack<Node*> s1, s2, s3;
while (l1 != NULL) {
s1.push(l1);
l1 = l1->next;
}
while (l2 != NULL) {
s2.push(l2);
l2 = l2->next;
}
int carry = 0;
while (!s1.empty() && !s2.empty()) {
int sum = s1.top()->data + s2.top()->data + carry;
Node* temp = newnode(sum % 10);
s3.push(temp);
if (sum > 9) {
carry = 1;
}
else {
carry = 0;
}
s1.pop();
s2.pop();
}
while (!s1.empty()) {
int sum = carry + s1.top()->data;
Node* temp = newnode(sum % 10);
s3.push(temp);
if (sum > 9) {
carry = 1;
}
else {
carry = 0;
}
s1.pop();
}
while (!s2.empty()) {
int sum = carry + s2.top()->data;
Node* temp = newnode(sum % 10);
s3.push(temp);
if (sum > 9) {
carry = 1;
}
else {
carry = 0;
}
s2.pop();
}
if (carry == 1) {
Node* temp = newnode(1);
s3.push(temp);
}
if (!s3.empty())
prev = s3.top();
while (!s3.empty()) {
Node* temp = s3.top();
s3.pop();
if (s3.size() == 0) {
temp->next = NULL;
}
else {
temp->next = s3.top();
}
}
return prev;
}

19. Rotate a Linked List

void rotate(Node** head_ref, int k)


{
if (k == 0)
return;
Node* current = *head_ref;
int count = 1;
while (count < k && current != NULL) {
current = current->next;
count++;
}
if (current == NULL)
return;
Node* kthNode = current;
while (current->next != NULL)
current = current->next;
current->next = *head_ref;
*head_ref = kthNode->next;
kthNode->next = NULL;
}

20. Delete N nodes after M nodes of a linked list

void skipMdeleteN(Node *head, int M, int N)


{
Node *curr = head, *t;
int count;
while (curr)
{
for (count = 1; count < M &&
curr!= NULL; count++)
curr = curr->next;
if (curr == NULL)
return;
t = curr->next;
for (count = 1; count<=N && t!= NULL; count++)
{
Node *temp = t;
t = t->next;
free(temp);
}
curr->next = t;
curr = t;
}
}

21. Pairwise swap elements of a given linked list by


changing links

node* pairWiseSwap(node* head)


{
if (head == NULL || head->next == NULL)
return head;
node* prev = head;
node* curr = head->next;
head = curr;
while (true) {
node* next = curr->next;
curr->next = prev;
if (next == NULL || next->next == NULL) {
prev->next = next;
break;
}
prev->next = next->next;
prev = next;
curr = prev->next;
}
return head;
}

node* pairWiseSwap(node* head)


{
if (head == NULL || head->next == NULL)
return head;
node* remaining = head->next->next;
node* newhead = head->next;
head->next->next = head;
head->next = pairWiseSwap(remaining);
return newhead;
}

22. Rearrange a linked list such that all even and odd
positioned nodes are together

Node *rearrangeEvenOdd(Node *head)


{
if (head == NULL)
return NULL;
Node *odd = head;
Node *even = head->next;
Node *evenFirst = even;
while (1)
{
if (!odd || !even || !(even->next))
{
odd->next = evenFirst;
break;
}
odd->next = even->next;
odd = even->next;
if (odd->next == NULL)
{
even->next = NULL;
odd->next = evenFirst;
break;
}

even->next = odd->next;
even = odd->next;
}
return head;
}
23. Rearrange a Linked List in Zig-Zag fashion

void zigZagList(L)
{
bool flag = true;
current = L;
while (L^.pas != NULL) {
if (flag)
{
if (current^.data > current^.next^.data) {
temp=current^.data;
current^.data = current^.next^.data
current^.next^.data=temp;
}
}
else
{
if (current^.data < current^.next^.data) {
temp=current^.data;
current^.data = current^.next^.data
current^.next^.data=temp;
}
}
current = current^.next;
flag = !flag;
}
}

node* zigzag(node* head, bool flag)


{
if (!head || !head->next)
return head;
if (flag == 1) {
if (head->data > head->next->data)
swap(head->data, head->next->data);
return zigzag(head->next, !flag);
}
else {
if (head->data < head->next->data)
swap(head->data, head->next->data);
return zigzag(head->next, !flag);
}
}

24. Add 1 to a number represented as linked list

int addWithCarry(Node* head)


{
if (head == NULL)
return 1;
int res = head->data + addWithCarry(head->next);
head->data = (res) % 10;
return (res) / 10;
}

Node* addOne(Node* head)


{
int carry = addWithCarry(head);
if (carry) {
Node* newNode = new Node;
newNode->data = carry;
newNode->next = head;
return newNode; // New node becomes head now
}
return head;
}

25. Point arbit pointer to greatest value right side node in


a linked list

Node* populateArbit(Node *head)


{
head = reverse(head);
Node *max = head;
Node *temp = head->next;
while (temp != NULL)
{
temp->arbit = max;
if (max->data < temp->data)
max = temp;
temp = temp->next;
}
return reverse(head);
}

26. Sort linked list which is already sorted on absolute


values

void populateArbit(Node *head)


{
static Node *maxNode;
if (head == NULL)
return;
if (head->next == NULL)
{
maxNode = head;
return;
}
populateArbit(head->next);
head->arbit = maxNode;
if (head->data > maxNode->data)
maxNode = head;
return;
}

27. Sort linked list which is already sorted on absolute


values

void sortList(Node** head)


{
Node* prev = (*head);
Node* curr = (*head)->next;
while (curr != NULL)
{
if (curr->data < prev->data)
{
prev->next = curr->next;
curr->next = (*head);
(*head) = curr;
curr = prev;
}
else
prev = curr;
curr = curr->next;
}
}

28. Delete last occurrence of an item from linked list

void deleteLast(Node* head, int key)


{
Node* x = NULL;
Node* temp = head;
while (temp) {
if (temp->key == key)
x = temp;
temp = temp->next;
}
if (x != NULL) {
x->key = x->next->key;
temp = x->next;
x->next = x->next->next;
delete temp;
}
}

29. Delete a Linked List node at a given position

void deleteNode(Node** head_ref, int position)


{
if (*head_ref == NULL)
return;
Node* temp = *head_ref;
if (position == 0) {
*head_ref = temp->next;
free(temp);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++)
temp = temp->next;
if (temp == NULL || temp->next == NULL)
return;
Node* next = temp->next->next;
free(temp->next); // Free memory
temp->next = next;
}

30. Decimal Equivalent of Binary Linked List

int decimalValue(Node *head)


{
int res = 0;
while (head != NULL)
{
res = (res << 1) + head->data;
head = head->next;
}
return res;
}

31. Remove every k-th node of the linked list

Node *deleteKthNode(struct Node *head, int k)


{
if (head == NULL)
return NULL;
if (k == 1)
{
freeList(head);
return NULL;
}
struct Node *ptr = head, *prev = NULL;
int count = 0;
while (ptr != NULL)
{
count++;
if (k == count)
{
delete(prev->next);
prev->next = ptr->next;
count = 0;
}
if (count != 0)
prev = ptr;
ptr = prev->next;
}

return head;
}

32. Multiply two numbers represented by Linked Lists

long long multiplyTwoLists (Node* first, Node* second)


{
long long N= 1000000007;
long long num1 = 0, num2 = 0;
while (first || second){
if(first){
num1 = ((num1)*10)%N + first->data;
first = first->next;
}
if(second)
{
num2 = ((num2)*10)%N + second->data;
second = second->next;
}
}
return ((num1%N)*(num2%N))%N;
}

33. Find the sum of last n nodes of the given Linked List

int sumOfLastN_NodesUtil(struct Node* head, int n)


{
if (n <= 0)
return 0;
stack<int> st;
int sum = 0;
while (head != NULL) {
st.push(head->data);
head = head->next;
}
while (n--) {
sum += st.top();
st.pop();
}
return sum;
}

int sumOfLastN_NodesUtil(struct Node* head, int n)


{
if (n <= 0)
return 0;
int sum = 0, len = 0;
struct Node* temp = head;
while (temp != NULL) {
len++;
temp = temp->next;
}
int c = len - n;
temp = head;
while (temp != NULL && c--)
temp = temp->next;
while (temp != NULL) {
sum += temp->data;
temp = temp->next;
}
return sum;
}

34. Count pairs from two linked lists whose sum is equal to
a given value

int countPairs(struct Node* head1, struct Node* head2, int x)


{
int count = 0;
struct Node *p1, *p2;
for (p1 = head1; p1 != NULL; p1 = p1->next)
for (p2 = head2; p2 != NULL; p2 = p2->next)
if ((p1->data + p2->data) == x)
count++;
return count;
}

35. Merge two sorted linked lists

Node* SortedMerge(Node* a, Node* b)


{
Node* result = NULL;
if (a == NULL)
return(b);
else if (b == NULL)
return(a);
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}

36. Merge a linked list into another linked list at


alternate positions

void merge(Node *p, Node **q)


{
Node *p_curr = p, *q_curr = *q;
Node *p_next, *q_next;
while (p_curr != NULL && q_curr != NULL)
{
p_next = p_curr->next;
q_next = q_curr->next;
q_curr->next = p_next;
p_curr->next = q_curr;
p_curr = p_next;
q_curr = q_next;
}

*q = q_curr; }

37. In-place Merge two linked lists without changing links


of first list

void mergeLists(struct Node *a, struct Node * &b)


{
while (a && b)
{
if (a->data > b->data)
{
swap(a->data, b->data);
struct Node *temp = b;
if (b->next && b->data > b->next->data)
{
b = b->next;
struct Node *ptr= b, *prev = NULL;
while (ptr && ptr->data < temp->data)
{
prev = ptr;
ptr = ptr -> next;
}
prev->next = temp;
temp->next = ptr;
}
}
a = a->next;
}
}

38. Delete middle of linked list


struct Node* deleteMid(struct Node* head)
{
if (head == NULL)
return NULL;
if (head->next == NULL) {
delete head;
return NULL;
}
struct Node* slow_ptr = head;
struct Node* fast_ptr = head;
struct Node* prev;
while (fast_ptr != NULL
&& fast_ptr->next != NULL) {
fast_ptr = fast_ptr->next->next;
prev = slow_ptr;
slow_ptr = slow_ptr->next;
}
prev->next = slow_ptr->next;
delete slow_ptr;
return head;
}

39. Merge two sorted lists (in-place)

Node* merge(Node* h1, Node* h2)


{
if (!h1)
return h2;
if (!h2)
return h1;
if (h1->data < h2->data) {
h1->next = merge(h1->next, h2);
return h1;
}
else {
h2->next = merge(h1, h2->next);
return h2;
}
}

40. Insert a node after the n-th node from the end

void insertAfterNthNode(Node* head, int n, int x)


{
if (head == NULL)
return;
Node* newNode = getNode(x);
Node* ptr = head;
int len = 0, i;
while (ptr != NULL) {
len++;
ptr = ptr->next;
}
ptr = head;
for (i = 1; i <= (len - n); i++)
ptr = ptr->next;
newNode->next = ptr->next;
ptr->next = newNode;}

41. Count rotations in sorted and rotated linked list

int countRotation(struct Node* head)


{
int count = 0;
int min = head->data;
while (head != NULL) {
if (min > head->data)
break;
count++;
head = head->next;
}
return count;
}

42. Make middle node head in a linked list

void setMiddleHead(Node** head)


{
if (*head == NULL)
return;
Node* one_node = (*head);
Node* two_node = (*head);
Node* prev = NULL;
while (two_node != NULL && two_node->next != NULL)
{
prev = one_node;
two_node = two_node->next->next;
one_node = one_node->next;
}
prev->next = prev->next->next;
one_node->next = (*head);
(*head) = one_node;
}

43.Move all occurrences of an element to end in a linked list

void moveToEnd(Node* head, int key)


{
struct Node* pKey = head;
struct Node* pCrawl = head;
while (pCrawl != NULL) {
if (pCrawl != pKey && pCrawl->data != key) {
pKey->data = pCrawl->data;
pCrawl->data = key;
pKey = pKey->next;
}
if (pKey->data != key)
pKey = pKey->next;
pCrawl = pCrawl->next;
}
}

44. Union and Intersection of two Linked Lists

bool isPresent(struct Node* head, int data)


{
struct Node* t = head;
while (t != NULL) {
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}
struct Node* getUnion(
struct Node* head1,
struct Node* head2)
{
struct Node* result = NULL;
struct Node *t1 = head1, *t2 = head2;
while (t1 != NULL) {
push(&result, t1->data);
t1 = t1->next;
}
while (t2 != NULL) {
if (!isPresent(result, t2->data))
push(&result, t2->data);
t2 = t2->next;
}
return result;
}
struct Node* getIntersection(struct Node* head1,
struct Node* head2)
{
struct Node* result = NULL;
struct Node* t1 = head1;
while (t1 != NULL) {
if (isPresent(head2, t1->data))
push(&result, t1->data);
t1 = t1->next;}
return result;}

You might also like