|
| 1 | +// Copyright (c) 2008 CareerMonk Publications and others. |
| 2 | +// #E-Mail : info@careermonk.com |
| 3 | +// Creation Date : 2008-01-10 06:15:46 |
| 4 | +// Created by : Narasimha Karumanchi |
| 5 | +// Book Title : Data Structures And Algorithms Made Easy |
| 6 | +// Warranty : This software is provided "as is" without any |
| 7 | +// warranty; without even the implied warranty of |
| 8 | +// merchantability or fitness for a particular purpose. |
| 9 | + |
| 10 | +#include <stdio.h> |
| 11 | +#include <stdlib.h> |
| 12 | + |
| 13 | +/* A structure of linked list ListNode */ |
| 14 | +struct ListNode { |
| 15 | + int data; |
| 16 | + struct ListNode *next; |
| 17 | +} *head1, *head2; |
| 18 | + |
| 19 | +void initialize(){ |
| 20 | + head1 = NULL; |
| 21 | + head2 = NULL; |
| 22 | +} |
| 23 | + |
| 24 | +/* |
| 25 | +Given a Inserts a ListNode in pTemp of a singly linked list. |
| 26 | +*/ |
| 27 | +void insert(struct ListNode **head, int data) { |
| 28 | + /* Create a new Linked List ListNode */ |
| 29 | + struct ListNode* newListNode = (struct ListNode*) malloc(sizeof(struct ListNode)); |
| 30 | + newListNode->data = data; |
| 31 | + /* Next pointer of new ListNode will point to head ListNode of linked list */ |
| 32 | + newListNode->next = *head; |
| 33 | + /* make new ListNode as new head of linked list */ |
| 34 | + *head = newListNode; |
| 35 | +} |
| 36 | + |
| 37 | +int getLength(struct ListNode *head){ |
| 38 | + /* Input Validation */ |
| 39 | + if (head == NULL) { |
| 40 | + printf("Error : Invalid ListNode pointer !!!\n"); |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + int length =0; |
| 45 | + while(head != NULL){ |
| 46 | + head = head->next; |
| 47 | + length++; |
| 48 | + } |
| 49 | + return length; |
| 50 | +} |
| 51 | + |
| 52 | +// Prints a linked list from head ListNode till tail ListNode |
| 53 | +void printLinkedList(struct ListNode *head) { |
| 54 | + while (head != NULL) { |
| 55 | + printf("%d", head->data); |
| 56 | + head = head->next; |
| 57 | + if(head != NULL) |
| 58 | + printf("-->"); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/* Reverses a given linked list, and return the head pointer of reversed linked list */ |
| 63 | +struct ListNode* reverseLinkedList(struct ListNode *head) { |
| 64 | + struct ListNode *previous, *current, *next; |
| 65 | + previous = NULL; |
| 66 | + current = head; |
| 67 | + |
| 68 | + while (current != NULL) { |
| 69 | + next = current->next; |
| 70 | + current->next = previous; |
| 71 | + previous = current; |
| 72 | + current = next; |
| 73 | + } |
| 74 | + return previous; |
| 75 | +} |
| 76 | + |
| 77 | +struct ListNode * intersectingListNode(struct ListNode * head1, struct ListNode * head2){ |
| 78 | + // get count of both the lists |
| 79 | + int m = getLength(head1); |
| 80 | + int n = getLength(head2); |
| 81 | + |
| 82 | + //to store the merge point |
| 83 | + struct ListNode * mergePoint = NULL; |
| 84 | + |
| 85 | + // finding the value of d based on the longer list |
| 86 | + int diff = (m > n) ? (m-n) : (n-m); |
| 87 | + |
| 88 | + //traverse the smaller longer list for 'diff' steps |
| 89 | + if(m > n){ |
| 90 | + while(diff--) |
| 91 | + head1 = head1 -> next; |
| 92 | + } |
| 93 | + else{ |
| 94 | + while(diff--) |
| 95 | + head2 = head2 -> next; |
| 96 | + } |
| 97 | + |
| 98 | + // now both lists have equal ListNodes till the end. |
| 99 | + while(head1 && head2){ |
| 100 | + if(head1 -> next->data == head2 -> next->data){ |
| 101 | + mergePoint = head1 -> next; |
| 102 | + break; |
| 103 | + } |
| 104 | + |
| 105 | + head1 = head1 -> next; |
| 106 | + head2 = head2 -> next; |
| 107 | + } |
| 108 | + |
| 109 | + return mergePoint; |
| 110 | +} |
| 111 | + |
| 112 | +int main() { |
| 113 | + struct ListNode *intersectingNode; |
| 114 | + initialize(); |
| 115 | + /* Creating a linked List*/ |
| 116 | + insert(&head1, 3); |
| 117 | + insert(&head1, 8); |
| 118 | + insert(&head1, 12); |
| 119 | + insert(&head1, 0); |
| 120 | + insert(&head1, 35); |
| 121 | + insert(&head1, 6); |
| 122 | + insert(&head1, 10); |
| 123 | + insert(&head1, 350); |
| 124 | + insert(&head1, 16); |
| 125 | + insert(&head1, 19); |
| 126 | + head1 = reverseLinkedList(head1); |
| 127 | + printf("\nLinked List\n"); |
| 128 | + printLinkedList(head1); |
| 129 | + insert(&head2, 13); |
| 130 | + insert(&head2, 18); |
| 131 | + insert(&head2, 112); |
| 132 | + insert(&head2, 10); |
| 133 | + insert(&head2, 135); |
| 134 | + insert(&head2, 16); |
| 135 | + insert(&head2, 10); |
| 136 | + insert(&head2, 350); |
| 137 | + insert(&head2, 16); |
| 138 | + insert(&head2, 19); |
| 139 | + head2 = reverseLinkedList(head2); |
| 140 | + printf("\nLinked List\n"); |
| 141 | + printLinkedList(head2); |
| 142 | + intersectingNode = intersectingListNode(head1, head2); |
| 143 | + if (intersectingListNode) |
| 144 | + printf("\n Intersecting ListNode is %d", intersectingNode->data); |
| 145 | + else |
| 146 | + printf("\n NULL \n"); |
| 147 | + return 0; |
| 148 | +} |
0 commit comments