-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked.java
More file actions
168 lines (144 loc) · 4.14 KB
/
Linked.java
File metadata and controls
168 lines (144 loc) · 4.14 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.Scanner;
public class Linked {
public class Node{
int data;
Node next;
Node(int data){
this.next=null;
this.data=data;
}
}
Node head=null;
void beg(int data){
Node newnode=new Node(data);
newnode.next=head;
head=newnode;
}
void end(int data){
Node newnode=new Node(data);
if(head==null){
head=newnode;
return;
}
Node temp=head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=newnode;
}
void pos(int data,int position){
if(position==1){
beg(data);
return;
}
Node temp=head;
Node newnode=new Node(data);
for(int i=1;i<position-1;i++){
temp=temp.next;
}
newnode.next=temp.next;
temp.next=newnode;
}
void traverse() {
if (head == null) {
System.out.println("List is empty");
return;
}
Node temp = head;
System.out.print("Linked List: ");
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}
void join(Node n1,Node n2){
if(n1==null){
n1=n2;
return;
}
while(n1.next!=null){
n1=n1.next;
}
n1.next=n2;
}
static Node merge(Node n1,Node n2){
if(n1==null){
return n2;
}
if(n2==null){
return n1;
}
Node mergedNode;
if(n1.data<=n2.data){
mergedNode=n1;
mergedNode.next=merge(n1.next,n2);
}
else{
mergedNode=n2;
mergedNode.next=merge(n1,n2.next);
}
return mergedNode;
}
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
Linked list1=new Linked();
Linked list2=new Linked();
list1.end(10);
list1.end(20);
list1.end(30);
list2.end(15);
list2.end(25);
list2.end(35);
System.out.println(list1.head.data);
list1.traverse();
list2.traverse();
list1.merge(list1.head,list2.head);
list1.traverse();
// System.out.println("--first linked list--");
// list1.traverse();
// System.out.println("--second linked list--");
// list2.traverse();
// list1.join(list1.head, list2.head);
// list1.traverse();
// while (true){
// System.out.println("\n--- MENU ---");
// System.out.println("1. Insert at Beginning");
// System.out.println("2. Insert at End");
// System.out.println("3. Insert at Position");
// System.out.println("4. traverse");
// System.out.println("5. exit");
// System.out.println("enter choice: ");
// int choice=sc.nextInt();
// switch(choice){
// case 1:
// System.out.println("enter data beg");
// int data1=sc.nextInt();
// list.beg(data1);
// break;
// case 2:
// System.out.println("enter data end");
// int data2=sc.nextInt();
// list.end(data2);
// break;
// case 3:
// System.out.println("enter pos");
// int position=sc.nextInt();
// System.out.println("enter data at pos");
// int data3=sc.nextInt();
// list.pos(data3,position);
// break;
// case 4:
// System.out.println(". list");
// list.traverse();
// break;
// case 5:
// System.out.println("exiting");
// sc.close();
// return;
// default:
// System.out.println("inavlid choice");
// }
// }
}
}