-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Leetcode 2807 problem solution simple
- Loading branch information
1 parent
0208861
commit ba59ebd
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class ListNode { | ||
int val; // data | ||
ListNode next; // pointer | ||
ListNode() {} //constructors | ||
ListNode(int val) { this.val = val; } | ||
ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
} | ||
|
||
|
||
class Solution { | ||
public ListNode insertGreatestCommonDivisors(ListNode head) { | ||
if( head.next==null) return head; | ||
|
||
ListNode node1=head; | ||
ListNode node2=head.next; | ||
|
||
while( node2!= null){ | ||
int gcdValue=calculateGCD(node1.val, node2.val); | ||
ListNode gcdNode=new ListNode(gcdValue); | ||
node1.next=gcdNode; | ||
gcdNode.next=node2; | ||
|
||
node1=node2; | ||
node2=node2.next; | ||
} | ||
return head; | ||
} | ||
private int calculateGCD(int a, int b){ | ||
while(b!=0){ | ||
int temp=b; | ||
b=a%b; | ||
a=temp; | ||
} | ||
int[] c={a}; | ||
return c; | ||
} | ||
} | ||
public class Leetcode_2807{ | ||
public static void main(String args[]){ | ||
int[] arr={1,2,3,4}; | ||
ListNode head=new ListNode(arr); | ||
int b=insertGreatestCommonDivisors(head); | ||
System.out.println(b); | ||
} | ||
} |