-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion138.java
More file actions
86 lines (71 loc) · 1.55 KB
/
Copy pathQuestion138.java
File metadata and controls
86 lines (71 loc) · 1.55 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 138
*/
public class Question138
{
public static Node copyRandomList(Node head)
{
if(head == null)
return null;
if(head.next == null)
return new Node(head.val);
Node temp = head;
int count= 0 ;
while(head!=null)
{
count++;
head = head.next;
}
head = temp;
System.out.println(count);
return null;
}
public static int find(ListNode[] array, ListNode node)
{
if(array==null || array.length==0)
return -1;
if(node == null)
return -1;
for(int i = 0;i<array.length;i++)
if(array[i] == node)
return i;
return -1;
}
public static ListNode copyRandomList(ListNode head)
{
if(head == null)
return null;
if(head.next == null)
return new ListNode(head.val);
ListNode temp = head;
int count= 0 ;
while(head!=null)
{
count++;
head = head.next;
}
head = temp;
System.out.println("Length is :"+count);
ListNode[] array = new ListNode[count];
int index = 0;
ListNode[] array1 = new ListNode[count];
boolean flag = true;
while(temp!=null)
{
array[index++] = temp;
ListNode newNode = new ListNode(temp.val);
temp = temp.next;
}
return null;
}
public static void main(String[] args)
{
System.out.println("Main Method starts");
ListNode head = LinkedList.create(10,"unsorted");
LinkedList.display(head);
ListNode res = copyRandomList(head);
LinkedList.display(res);
}
}