-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo1.java
More file actions
60 lines (54 loc) · 2.2 KB
/
Demo1.java
File metadata and controls
60 lines (54 loc) · 2.2 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
public class demo1 {
ListNode head;
private int data;
static class ListNode{
private int data;
private ListNode next;
}
void ListNode(int data){
this.data=data;
ListNode next = null;
}
public ListNode reverse (ListNode head){
if (head == null){
return head;
}
ListNode current;
ListNode previous = null;
Listnode next = null;
while (current != null){
next= current.next;
current.next=previous;
previous = current;
current=next;
}
return previous;
}
void display(){
ListNode current = head;
while(current!=null){
System.out.print(current.data + "");
current=current.next;
}
System.out.print("null");
}
public static void main(String[] args){
ListNode head = new ListNode(1);
ListNode second = new ListNode(5);
ListNode third = new ListNode(1);
ListNode fourth = new ListNode(2);
ListNode fifth = new ListNode(3);
ListNode sixth = new ListNode(4);
ListNode seventh = new ListNode(5);
head.next = second;
second.next=third;
third.next=fourth;
fourth.next=fifth;
fifth.next=sixth;
sixth.next=seventh;
Reverse r = new Reverse();
Reverse.display(head);
ListNode reverseList = r.reverse(head);
r.display(reverseList);
}
}