-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq2p4.java
More file actions
51 lines (47 loc) · 1.19 KB
/
Copy pathq2p4.java
File metadata and controls
51 lines (47 loc) · 1.19 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
public class q2p4{
public static void main(String[] args){
Node head = new Node(0);
int cnt = 1;
while(cnt<=10){
head.append(cnt++);
}
Node buf = head.change(head,6);
while(buf != null){
System.out.println(buf.value);
buf = buf.next;
}
}
}
class Node{
Node next = null;
int value;
public Node(int value){
this.value = value;
}
public void append(int value){
Node end = new Node(value);
Node previous = this;
while(previous.next != null){
previous = previous.next;
}
previous.next = end;
}
public Node change(Node node, int x){
Node head = node;
Node tail = node;
while(node != null){
Node buf = node.next;
if(node.value>=x){
node.next = head;
head = node;
}else{
tail.next = node;
tail = node;
}
node = buf;
}
tail.next = null;
return head;
} //we must use return head here,
//because we cannot modify the address value about the input parameter.
}