-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlasttofirst.java
More file actions
68 lines (64 loc) · 1.4 KB
/
lasttofirst.java
File metadata and controls
68 lines (64 loc) · 1.4 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
//moving last element to the front of the linked list
public class Lastoffront
{
Node head;
class Node
{
int data;
Node next;
Node(int d)
{
data=d;
next=null;
}
}
//function to insert an element into a list
void insert(int element)
{
Node n = new Node(element);
n.next=head;
head=n;
}
//function to display the list
void print()
{
Node h = head;
while(h!=null)
{
System.out.print(h.data+" ");
h=h.next;
}
System.out.println();
}
//function to move the last element to the front
void frontmove()
{
if(head==null || head.next==null)
return;
Node seclast =null;
Node temp=head;
while(temp.next!=null)
{
seclast = temp;
temp=temp.next;
}
seclast.next=null;
temp.next = head;
head=temp;
}
public static void main(String args[])
{
Lastoffront lf = new Lastoffront();
lf.insert(1);
lf.insert(2);
lf.insert(3);
lf.insert(4);
lf.insert(5);
lf.insert(6);
System.out.println("Initial list : ");
lf.print();
System.out.println("Final rearranged list ");
lf.frontmove();
lf.print();
}
}