-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion1019.java
More file actions
64 lines (57 loc) · 1.05 KB
/
Copy pathQuestion1019.java
File metadata and controls
64 lines (57 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 1019
*/
public class Question1019
{
public static int[] nextLargerNodes(ListNode head)
{
ListNode temp = head;
int length = 0;
while(head!=null)
{
length++;
head = head.next;
}
head = temp;
int[] arr = new int[length];
if(head.next==null)
{
arr[0] = 0;
return arr;
}
ListNode pointer = head;
int index = 0;
int value = 0;
while(pointer!=null)
{
temp = pointer.next;
value = pointer.val;
while(temp!=null)
{
if(temp.val>pointer.val)
{
pointer.val = temp.val;
arr[index++] = temp.val;
break;
}
temp = temp.next;
}
if(value == pointer.val)
{
pointer.val = 0;
arr[index++] = 0;
}
pointer = pointer.next;
}
return arr;
}
public static void main(String[] args)
{
ListNode head = LinkedList.create(5,"unsorted");
LinkedList.display(head);
int[] result = nextLargerNodes(head);
Array.display(result);
}
}