Related Tags

communitycreator

# How to find the merge point for two linked lists

Dhruv Sharma

### Overview

A linked list is a collection of nodes. Each node contains data and a pointer to the next node.

A linked list is preferred over other data structures like arrays because it has a dynamic size and it's easier to insert and delete elements/nodes.

class Node:
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):
self.tail = None

def print_ll(self):
while temp:
print(str(temp.data))
temp = temp.next

def insert(self, data):
node = Node(data)
else:
self.tail.next = node
self.tail = node

l1val = [1,2,3,4,5]

for i in l1val:
l1.insert(i)

l1.print_ll()

### Explanation

In a linked list, the head attribute refers to the first node of a list. All the functions used in the list are added in the SinglyLinkedList class.

Node class is used to create a new node, each node has a data attribute and a pointer attribute to the next node which initially points to a null.

### Understanding the Problem

Consider two linked lists that will meet at some point. The head nodes of both lists are given pointers to find the node where the two lists merge.

Creating the merge for the linked lists

### Example

In the above example, the two lists merge at Node 4, which is the result of the function.

### Solution

In the program below, we use the merge_point() function to find the data of the node where both the lists merge.

class SinglyLinkedListNode:
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):
self.tail = None

def insert(self, data):
else:
self.tail.next = node
self.tail = node

data = []

l1val = [1,2,3,4,5]
l1_count = 5
for i in l1val:
l1.insert(i)

l2val = [9,2]
l2_count = 2
for i in l2val:
l2.insert(i)

for i in range(l1_count):
if i < 3:
ptr1 = ptr1.next

for i in range(l2_count):
if i != l2_count-1:
ptr2 = ptr2.next
ptr2.next = ptr1


Finding merge point of two linked lists

### Explanation

We follow the steps below to run the merge_point() function:

Lines 21–23: Traverse the first linked list and add all the nodes in a list called data

Lines 24–25: Check if any node exists in the data list to traverse the second linked list.

Lines 25–26: Use an exit condition to return the data of the node which exist in the data list.

RELATED TAGS

communitycreator
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.educative.io/answers/how-to-find-the-merge-point-for-two-linked-lists", "fetch_time": 1660469973000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2022-33/segments/1659882572021.17/warc/CC-MAIN-20220814083156-20220814113156-00601.warc.gz", "warc_record_offset": 639100951, "warc_record_length": 72837, "token_count": 616, "char_count": 2356, "score": 3.515625, "int_score": 4, "crawl": "CC-MAIN-2022-33", "snapshot_type": "latest", "language": "en", "language_score": 0.7523612976074219, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00052-of-00064.parquet"}