-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInsertAtTail.java
73 lines (64 loc) · 1.84 KB
/
InsertAtTail.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
/**
*
* Naive sol would be to start from head and traverse till last node and then insert newNode but it will take O(n) time
*
* Eff sol would be maintain tail ptr
*
* Another eff sol would be
* insert newNode after head, then we swap contents of head of newNode, and we now need to change the head to head.next, we are done, time: O(1)
*/
public class InsertAtTail {
public static void main(String[] args) {
Node head = new Node(10);
head.next = head;
head = eff_insertAtTail(head, 20);
head = eff_insertAtTail(head, 30);
head = eff_insertAtTail(head, 15);
printList(head);
}
public static Node insertAtTail(Node head, int x)
{
Node newNode = new Node(x);
if (head == null)
return newNode;
Node curr = head;
while (curr.next != head)
curr = curr.next;
curr.next = newNode;
newNode.next = head;
return head;
}
public static Node eff_insertAtTail(Node head, int x) {
Node newNode = new Node(x);
if(head==null)
{
newNode.next = newNode;
return newNode;
}
else {
// insert newNode after head
newNode.next = head.next;
head.next = newNode;
// swapping
int t = newNode.data;
newNode.data = head.data;
head.data = t;
// now change the head
head = head.next;
return head;
}
}
public static void printList(Node head) {
if (head == null)
return;
System.out.print(head.data + "->");
for (Node r = head.next; r != head; r = r.next)
System.out.print(r.data + "->");
System.out.print(head.data + "\n");
}
}
/**
* op
* 200->10->200
* 200->10->20->30->200
*/