1+ // https://leetcode.com/problems/design-linked-list/
2+ //
3+ // algorithms
4+ // Easy (20.84%)
5+ // Total Accepted: 34,842
6+ // Total Submissions: 167,186
7+ // beats 100.0% of java submissions
8+
9+
10+ class ListNode {
11+ int val ;
12+ ListNode next ;
13+ ListNode prev ;
14+
15+ ListNode (int val ) {
16+ this .val = val ;
17+ }
18+ }
19+
20+ class MyLinkedList {
21+
22+ int size ;
23+ ListNode head ;
24+ ListNode tail ;
25+
26+ /** Initialize your data structure here. */
27+ public MyLinkedList () {
28+
29+ }
30+
31+ /**
32+ * Get the value of the index-th node in the linked list. If the index is
33+ * invalid, return -1.
34+ */
35+ public int get (int index ) {
36+ ListNode n = getNode (index );
37+ if (n == null ) {
38+ return -1 ;
39+ }
40+
41+ return n .val ;
42+ }
43+
44+ public ListNode getNode (int index ) {
45+ if (index < 0 || index >= size ) {
46+ return null ;
47+ }
48+
49+ if (index <= size / 2 ) {
50+ ListNode tmp = head ;
51+ for (int i = 0 ; i < index ; i ++) {
52+ tmp = tmp .next ;
53+ }
54+ return tmp ;
55+ }
56+
57+ ListNode tmp = tail ;
58+ for (int i = size - 1 ; i > index ; i --) {
59+ tmp = tmp .prev ;
60+ }
61+
62+ return tmp ;
63+ }
64+
65+ /**
66+ * Add a node of value val before the first element of the linked list. After
67+ * the insertion, the new node will be the first node of the linked list.
68+ */
69+ public void addAtHead (int val ) {
70+ ListNode n = new ListNode (val );
71+ n .next = head ;
72+ if (head != null ) {
73+ head .prev = n ;
74+ } else {
75+ tail = n ;
76+ }
77+ head = n ;
78+ size += 1 ;
79+ }
80+
81+ /** Append a node of value val to the last element of the linked list. */
82+ public void addAtTail (int val ) {
83+ ListNode n = new ListNode (val );
84+ n .prev = tail ;
85+ if (tail != null ) {
86+ tail .next = n ;
87+ } else {
88+ head = n ;
89+ }
90+ tail = n ;
91+ size += 1 ;
92+ }
93+
94+ public void deleteAtHead () {
95+ if (size == 0 ) {
96+ return ;
97+ }
98+ head = head .next ;
99+ if (head != null ) {
100+ head .prev = null ;
101+ } else {
102+ tail = null ;
103+ }
104+ size -= 1 ;
105+ }
106+
107+ public void deleteAtTail () {
108+ if (size == 0 ) {
109+ return ;
110+ }
111+ tail = tail .prev ;
112+ if (tail != null ) {
113+ tail .next = null ;
114+ } else {
115+ head = null ;
116+ }
117+ size -= 1 ;
118+ }
119+
120+ /**
121+ * Add a node of value val before the index-th node in the linked list. If index
122+ * equals to the length of linked list, the node will be appended to the end of
123+ * linked list. If index is greater than the length, the node will not be
124+ * inserted.
125+ */
126+ public void addAtIndex (int index , int val ) {
127+ if (index > size ) {
128+ return ;
129+ }
130+ index = Math .max (index , 0 );
131+ if (index == 0 ) {
132+ addAtHead (val );
133+ } else if (index == size ) {
134+ addAtTail (val );
135+ } else {
136+ ListNode n = getNode (index );
137+ ListNode newNode = new ListNode (val );
138+ newNode .prev = n .prev ;
139+ n .prev .next = newNode ;
140+ newNode .next = n ;
141+ n .prev = newNode ;
142+ size ++;
143+ }
144+ }
145+
146+ /** Delete the index-th node in the linked list, if the index is valid. */
147+ public void deleteAtIndex (int index ) {
148+ ListNode n = getNode (index );
149+ if (n == null ) {
150+ return ;
151+ }
152+ if (index == 0 ) {
153+ deleteAtHead ();
154+ } else if (index == size - 1 ) {
155+ deleteAtTail ();
156+ } else {
157+ ListNode prev = n .prev , next = n .next ;
158+ n = null ;
159+ prev .next = next ;
160+ next .prev = prev ;
161+ size -= 1 ;
162+ }
163+ }
164+ }
165+
166+ /**
167+ * Your MyLinkedList object will be instantiated and called as such:
168+ * MyLinkedList obj = new MyLinkedList(); int param_1 = obj.get(index);
169+ * obj.addAtHead(val); obj.addAtTail(val); obj.addAtIndex(index,val);
170+ * obj.deleteAtIndex(index);
171+ */
0 commit comments