-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppend_Last_to_first.java
78 lines (76 loc) · 1.75 KB
/
Append_Last_to_first.java
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
package Programs2;
import java.util.Scanner;
class node<T> {
T data;
node<T> next;
node(T d) {
data = d;
}
}
public class Append_Last_to_first {
static node createLinkedList() {
Scanner s = new Scanner(System.in);
System.out.print("Enter data : ");
int data = s.nextInt();
node head = null;
node temp=null;
while (data != -1) {
node node = new node(data);
if (head == null) {
head = node;
temp=head;
} else {
temp.next=node;
temp=temp.next;
}
System.out.print("Enter next data : ");
data = s.nextInt();
}
return head;
}
static void display(node head)
{
node temp=head;
while(temp!=null){
System.out.print(temp.data+" ");
temp= temp.next;
}
}
public static node<Integer> append(node<Integer> root, int n) {
node<Integer> temp=root,temp1=root,temp2=null,temp3=null;
int count=0,c=0;
while(temp!=null)
{
count++;
temp=temp.next;
}
c=count;
temp=root;
while(c>n)
{
temp1=temp;
temp=temp.next;
c--;
}
temp3=temp;
temp1.next=null;
while(temp.next!=null)
temp=temp.next;
temp.next=root;
return temp3;
}
public static void main(String[] args) {
node head = createLinkedList();
Scanner s=new Scanner(System.in);
node temp = head;
System.out.println();
System.out.print("Linked List formed is : ");
display(temp);
System.out.println();
System.out.print("How many last elements of the List you want to append to front : ");
int k=s.nextInt();
temp=append(temp, k);
System.out.print("List after appending Last "+k+" elements to front is : ");
display(temp);
}
}