Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode* res = new ListNode(0);
ListNode* current = res;
while (true){
if (!l1){
current->next = l2;
break;
}
if (!l2){
current->next = l1;
break;
}
if (l1->val < l2->val){
current->next = l1;
l1 = l1->next;
}else{
current->next = l2;
l2 = l2->next;
}
current = current->next;
}
return res->next;
}
};
struct node *getUnion(struct node *head1, struct node *head2)
unordered_set<int> seen;
while(temp1->next)
seen.insert(temp1->data);
temp1=temp1->next;
seen.insert(temp1->data);
temp1->next=NULL;
while(temp2)
if(seen.find(temp2->data)==seen.end())
seen.insert(temp2->data);
temp1->next=temp2;
temp1=temp2;
temp2=temp2->next;
temp1->next=NULL;
return head1;
}
1. /* Function to get union of two linked lists head1 and
head2 */
2. struct node *getUnion(struct node *head1, struct node
*head2)
3. {
4. struct node *res=NULL;
5. map<int,bool> m;
6. struct node *curr=head1;
7. while(curr)
8. {
9. if(!m[curr->data])
10. {
11. m[curr->data]=true;
12. push(&res,curr->data);
13. }
14. curr=curr->next;
15. }
16. curr=head2;
17. while(curr)
18. {
19. if(!m[curr->data])
20. {
21. m[curr->data]=true;
22. push(&res,curr->data);
23. }
24. curr=curr->next;
25. }
26. return res;
27. }
28.
29. /* Function to get intersection of two linked lists
30. head1 and head2 */
31. struct node *getIntersection(struct node *head1,
32. struct node *head2)
33. {
34. struct node *res=NULL;
35. map<int,bool> m;
36. struct node *curr=head1;
37. while(curr)
38. {
39. if(!m[curr->data])
40. m[curr->data]=true;
41. curr=curr->next;
42. }
43. curr=head2;
44. while(curr)
45. {
46. if(m[curr->data])
47. push(&res,curr->data);
48. curr=curr->next;
49. }
50.
51. return res;
52. }
Swap nodes in a linked list without swapping data
following cases to be handled.
1) x and y may or may not be adjacent.
2) Either x or y may be a head node.
3) Either x or y may be last node.
4) x and/or y may not be present in linked list.
return -1;
}
Reverse a Linked List in groups of given size
struct Node *reverse (struct Node *head, int k)
{
struct Node* current = head;
struct Node* next = NULL;
struct Node* prev = NULL;
int count = 0;
while(curr != NULL)
{
thisHead = curr;
int count = 0;
prev = NULL;
while(curr != NULL && count < k)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
++count;
}
if(prevHead != NULL)
prevHead->next = prev;
else
head = prev;
prevHead = thisHead;
}
return head;
}
return result;
}
// Traverse list
while (curr != NULL)
{
// If curr is smaller than prev, then
// it must be moved to head
if (curr->data < prev->data)
{
// Detach curr from linked list
prev->next = curr->next;
// Update current
curr = prev;
}
// Move current
curr = curr->next;
}
}
Swap Kth node from beginning with Kth node from
end in a Linked List
Following are the interesting cases that must be handled.
1) Y is next to X
2) X is next to Y
3) X and Y are same
4) X and Y dont exist (k is more than number of nodes in linked list)
void swapKth(struct Node **head_ref, int k)
{
// Count nodes in linked list
int n = countNodes(*head_ref);
// Check if k is valid
if (n < k) return;
// If x (kth node from start) and y(kth node from end) are same
if (2*k - 1 == n) return;
// Find the kth node from beginning of linked list. We also find
// previous of kth node because we need to update next pointer of
// the previous.
Node *x = *head_ref;
Node *x_prev = NULL;
for (int i = 1; i < k; i++)
{
x_prev = x;
x = x->next;
}
// Similarly, find the kth node from end and its previous. kth node
// from end is (n-k+1)th node from beginning
Node *y = *head_ref;
Node *y_prev = NULL;
for (int i = 1; i < n-k+1; i++)
{
y_prev = y;
y = y->next;
}
Dummy nodes.
void AlternatingSplit(struct Node* source, struct Node** aRef,
struct Node** bRef)
{
struct Node aDummy;
struct Node* aTail = &aDummy; /* points to the last node in 'a' */
struct Node bDummy;
struct Node* bTail = &bDummy; /* points to the last node in 'b' */
struct Node* current = source;
aDummy.next = NULL;
bDummy.next = NULL;
while (current != NULL)
{
MoveNode(&(aTail->next), ¤t); /* add at 'a' tail */
aTail = aTail->next; /* advance the 'a' tail */
if (current != NULL)
{
MoveNode(&(bTail->next), ¤t);
bTail = bTail->next;
}
}
*aRef = aDummy.next;
*bRef = bDummy.next;
}
Rearrange a linked list such that all even and odd
positioned nodes are together
make sure that all below cases are handled
1) Empty linked list
2) A linked list with only one node
3) A linked list with only two nodes
4) A linked list with odd number of nodes
5) A linked list with even number of nodes
Node *rearrangeEvenOdd(Node *head)
{
// Corner case
if (head == NULL)
return NULL;
while (1)
{
// If there are no more nodes, then connect
// first node of even list to the last node of odd list
if (!odd || !even || !(even->next))
{
odd->next = evenFirst;
break;
}
return head;
}
Merge a linked list into another linked list at
alternate positions
// Main function that inserts nodes of linked list q into p at
// alternate positions. Since head of first list never changes
// and head of second list may change, we need single pointer
// for first list and double pointer for second list.
void merge(struct Node *p, struct Node **q)
{
struct Node *p_curr = p, *q_curr = *q;
struct Node *p_next, *q_next;
ascn->next = NULL;
dscn->next = NULL;
*Ahead = (*Ahead)->next;
*Dhead = (*Dhead)->next;
}
Merge two sorted linked lists such that merged list
is in reverse order
Node* SortedMerge(Node *a, Node *b)
{
// If both lists are empty
if (a==NULL && b==NULL) return NULL;
return res;
}