Linked List in Java: 17 - Get Union set of two Linked Lists

  Рет қаралды 4,877

Coding Simplified

Coding Simplified

Күн бұрын

Пікірлер: 4
@MohitMaroliyaBCS
@MohitMaroliyaBCS 4 жыл бұрын
But sir if A = 1->2->2->2->3->3 and B = 2->2->2->2->2->2=3 then your ans returns 1->2->2->2->2->2->3->3 but the union of these two set is 1->2->3. SImply it does not take care of repeating values.
@Prince-yz1tc
@Prince-yz1tc 4 жыл бұрын
Why we have made a object of Node class temp? How it is preventing from null pointer exception.?
@CodingSimplified
@CodingSimplified 4 жыл бұрын
We made temp as object of Node class, so that we keep appending the smaller value in it & at last we can return the finalList.next. - Now if you don't want to create temp as new Object & use, that's also fine. Just that you need to update the code as following:. Updating the code in mergeWithUnion. You'll need a head & temp to operate this one: private Node mergeWithUnion(Node a, Node b) { Node head, temp; head = temp = null; while(a != null && b != null) { if(a.data < b.data) { if(head == null) { head = temp = a; } else { temp.next = a; temp = temp.next; } a = a.next; } else if(a.data > b.data) { if(head == null) { head = temp = b; } else { temp.next = b; temp = temp.next; } b = b.next; } else { if(head == null) { head = temp = a; } else { temp.next = a; temp = temp.next; } a = a.next; b = b.next; } } temp.next = (a == null) ? b : a; return head; } Let me know if it looks fine to you or if you've any further questions.
@amiransari3564
@amiransari3564 3 жыл бұрын
Hi , I run this code and returning wrong union values. Please could you check below input List1: 9 6 4 2 3 8 List2: 1 2 8 6 2 Output: 1 2 2 3 4 6 8 9
Linked List in Java: 18 - Get Intersection set of two Linked Lists
6:20
Coding Simplified
Рет қаралды 3,8 М.
LinkedList vs ArrayList in Java Tutorial - Which Should You Use?
11:43
Coding with John
Рет қаралды 607 М.
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 94 МЛН
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,1 МЛН
Каха и лужа  #непосредственнокаха
00:15
Understanding and implementing a Linked List in C and Java
18:15
Jacob Sorber
Рет қаралды 245 М.
L23. Merge two sorted Linked Lists
18:55
take U forward
Рет қаралды 71 М.
Disjoint Sets using union by rank and path compression Graph Algorithm
17:49
Tushar Roy - Coding Made Simple
Рет қаралды 315 М.
How to STUDY so FAST it feels like CHEATING
8:03
The Angry Explainer
Рет қаралды 1,8 МЛН
How to Insert a Node in Order in a Linked List
21:58
Gina Sprint
Рет қаралды 6 М.
Introduction to Linked Lists (Data Structures & Algorithms #5)
18:47
Binary Search : Median of two sorted arrays of different sizes.
24:48
Tushar Roy - Coding Made Simple
Рет қаралды 549 М.
Intersection of Two Linked Lists - Leetcode 160 - Python
8:13
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 970 М.
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 94 МЛН