-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdelete_last_node.java
58 lines (46 loc) · 1.16 KB
/
delete_last_node.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
/*
ip: 10->20->30->NULL
op: 10->20->NULL
ip: 10->NULL
op: NULL
ip: NULL
op: NULL
idea: stop at node whose next's next field is null, i.e stop at second last node, and then change the link of curr.next to null
*/
public class delete_last_node {
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
// printing before deleting a node
printList(head);
System.out.println();
head = delTail(head);
// printing after deleting the node
printList(head);
}
static Node delTail(Node head) {
if (head == null)
return head;
if (head.next == null)
return null;
Node currNode = head;
while(currNode.next.next != null)
currNode = currNode.next;
currNode.next = null;
return head;
}
public static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
}
}
/**
* Time: O(n)
* op
* 10 20 30
* 10 20
*/