Description
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
Implement the MyLinkedList class:
MyLinkedList(): Initializes theMyLinkedListobject.int get(int index): Get the value of theindexth node in the linked list. If theindexis invalid, return-1.void addAtHead(int val): Add a node of valuevalbefore the first element of the linked list. After the insertion, the new node will be the first node of the linked list.void addAtTail(int val): Append a node of valuevalas the last element of the linked list.void addAtIndex(int index, int val)Add a node of valuevalbefore theindexth node in the linked list. Ifindexequals the length of the linked list, the node will be appended to the end of the linked list. Ifindexis greater than the length, the node will not be inserted.void deleteAtIndex(int index): Delete theindexth node in the linked list, if theindexis valid.
Example 1:
1Input
2["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
3[[], [1], [3], [1, 2], [1], [1], [1]]
4Output
5[null, null, null, null, 2, null, 3]
6
7Explanation
8MyLinkedList myLinkedList = new MyLinkedList();
9myLinkedList.addAtHead(1);
10myLinkedList.addAtTail(3);
11myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
12myLinkedList.get(1); // return 2
13myLinkedList.deleteAtIndex(1); // now the linked list is 1->3
14myLinkedList.get(1); // return 3
Constraints:
0 <= index, val <= 1000- Please do not use the built-in
LinkedListlibrary. - At most
2000calls will be made toget,addAtHead,addAtTail,addAtIndexanddeleteAtIndex.
Solution
1class ListNode {
2 int val;
3 ListNode next;
4 ListNode() {}
5 ListNode(int val) { this.val = val; }
6 ListNode(int val, ListNode next) { this.val = val; this.next = next; }
7}
8
9
10class MyLinkedList {
11
12 public ListNode head;
13 public int val;
14
15 public MyLinkedList() {
16 this.head = null;
17 this.val = 0;
18 }
19
20 public int get(int index) {
21 ListNode current = head;
22 while (current != null && index > 0) {
23 current = current.next;
24 index--;
25 }
26 return current == null ? -1 : current.val;
27 }
28
29 public void addAtHead(int val) {
30 ListNode newNode = new ListNode(val);
31 newNode.next = head;
32 head = newNode;
33 }
34
35 public void addAtTail(int val) {
36 ListNode newNode = new ListNode(val);
37 if (head == null) {
38 head = newNode;
39 return;
40 }
41 ListNode current = head;
42 while (current.next != null) {
43 current = current.next;
44 }
45 current.next = newNode;
46 }
47
48 public void addAtIndex(int index, int val) {
49 if (index == 0) {
50 addAtHead(val);
51 return;
52 }
53 ListNode newNode = new ListNode(val);
54 ListNode current = head;
55 while (current != null && index > 1) {
56 current = current.next;
57 index--;
58 }
59 if (current == null) {
60 return;
61 }
62 newNode.next = current.next;
63 current.next = newNode;
64 }
65
66 public void deleteAtIndex(int index) {
67 if (index == 0) {
68 head = head.next;
69 return;
70 }
71 ListNode current = head;
72 while (current != null && index > 1) {
73 current = current.next;
74 index--;
75 }
76 if (current == null || current.next == null) {
77 return;
78 }
79 current.next = current.next.next;
80 }
81}


Comments