Challenge

Project 1

2.

class LeadersInArray
{

void printLeaders(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
int j;
for (j = i + 1; j < size; j++)
{
if (arr[i] <=arr[j])
break;
}
if (j == size) // the loop didn't break
System.out.print(arr[i] + " ");
}
}


public static void main(String[] args)
{
LeadersInArray lead = new LeadersInArray();
int arr[] = new int[]{16, 17, 4, 3, 5, 2};
int n = arr.length;
lead.printLeaders(arr, n);
}
}

3. reverce link list

class LinkedList {

static Node head;

static class Node {

int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}


Node reverse(Node node)
{
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}

void printList(Node node)
{
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}


public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(20);
list.head.next = new Node(40);
list.head.next.next = new Node(60);
list.head.next.next.next = new Node(70);
list.head.next.next.next.next = new Node(50);
list.head.next.next.next.next.next = new Node(10);

System.out.println("Given Linked list");
list.printList(head);
head = list.reverse(head);
System.out.println("");
System.out.println("Reversed linked list ");
list.printList(head);
}
}

4.add two number

import java.util.*;

class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

class two{

static void printList(Node n){
while(n!=null){
System.out.print(n.data+" ");
n = n.next;
}
System.out.println();
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();

while (T-- > 0) {

int n = sc.nextInt();
int val = sc.nextInt();

Node first = new Node(val);
Node tail = first;
for(int i=0; i<n-1; i++)
{
val = sc.nextInt();
tail.next = new Node(val);
tail = tail.next;
}

int m = sc.nextInt();
val = sc.nextInt();

Node second = new Node(val);
tail = second;
for(int i=0; i<m-1; i++)
{
val = sc.nextInt();
tail.next = new Node(val);
tail = tail.next;
}

Solution g = new Solution();
Node res = g.addTwoLists(first, second);
printList(res);
}
}
}


class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

class Solution{

static Node addTwoLists(Node first, Node second){

Node curr=first;
Node prev=null;
Node temp=null;
while(curr!=null )
{
temp=curr.next;
curr.next=prev;
prev=curr;
curr=temp;
}
Node cur=second;
Node pre=null;
Node tem=null;
while(cur!=null )
{
tem=cur.next;
cur.next=pre;
pre=cur;
cur=tem;
}
first=prev;
second=pre;
int carry = 0;
Node head = null;
Node sum = null;

while (first != null || second != null || carry == 1)
int newVal = carry;

if (first!=null)
newVal += first.data;
if (second!=null)
newVal += second.data;

carry = newVal / 10; //to be used in the next node calculation
newVal = newVal % 10;

Node newNode = new Node(newVal);
newNode.next = sum;
sum = newNode;

if (first!=null) // initialising nodes for next iteration
first = first.next;
if (second!=null)
second = second.next;
}
return sum;
}

}

5

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.