LeetCode 707. Design Linked List

原题链接在这里:https://leetcode.com/problems/design-linked-list/

题目:

Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and nextval 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 these functions in your linked list class:

  • get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  • addAtHead(val) : Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
  • addAtTail(val) : Append a node of value val to the last element of the linked list.
  • addAtIndex(index, val) : Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
  • deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1, 2);  // linked list becomes 1->2->3
linkedList.get(1);            // returns 2
linkedList.deleteAtIndex(1);  // now the linked list is 1->3
linkedList.get(1);            // returns 3

Note:

  • All values will be in the range of [1, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in LinkedList library.

题解:

用double linked list node来实现.

Time Complexity: get, O(n). addAtHead, O(1). addAtTail, O(n). addAtIndex, O(n). deleteAtIndex, O(n).

Space: O(1). 

AC Java:

 1 class MyLinkedList {
 2     ListNode head;
 3     ListNode tail;
 4     int size;
 5     
 6     /** Initialize your data structure here. */
 7     public MyLinkedList() {
 8         head = new ListNode(-1);
 9         tail = new ListNode(1);
10         head.next = tail;
11         tail.prev = head;
12         
13         this.size = 0;
14     }
15     
16     /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
17     public int get(int index) {
18         if(index<0 || index>=this.size){
19             return -1;
20         }
21         
22         ListNode cur = this.head;
23         while(index-->0){
24             cur = cur.next;
25         }
26         
27         return cur.next.val;
28     }
29     
30     /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
31     public void addAtHead(int val) {
32         addAtIndex(0, val);
33     }
34     
35     /** Append a node of value val to the last element of the linked list. */
36     public void addAtTail(int val) {
37         addAtIndex(this.size, val);
38     }
39     
40     /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
41     public void addAtIndex(int index, int val) {
42         if(index>this.size){
43             return;
44         }
45         
46         ListNode cur = this.head;
47         while(index-- > 0){
48             cur = cur.next;
49         }
50         
51         ListNode addedNode = new ListNode(val);
52         addedNode.next = cur.next;
53         addedNode.prev = cur;
54         cur.next.prev = addedNode;
55         cur.next = addedNode;
56         
57         this.size++;
58     }
59     
60     /** Delete the index-th node in the linked list, if the index is valid. */
61     public void deleteAtIndex(int index) {
62         if(index<0 || index>=this.size){
63             return;
64         }
65         
66         ListNode cur = this.head;
67         while(index-- > 0){
68             cur = cur.next;
69         }
70         
71         ListNode deletedNode = cur.next;
72         cur.next = deletedNode.next;
73         deletedNode.next.prev = cur;
74         
75         this.size--;
76     }
77 }
78 
79 class ListNode{
80     int val;
81     ListNode prev;
82     ListNode next;
83     public ListNode(int val){
84         this.val = val;
85     }
86 }
87 
88 /**
89  * Your MyLinkedList object will be instantiated and called as such:
90  * MyLinkedList obj = new MyLinkedList();
91  * int param_1 = obj.get(index);
92  * obj.addAtHead(val);
93  * obj.addAtTail(val);
94  * obj.addAtIndex(index,val);
95  * obj.deleteAtIndex(index);
96  */
原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/10965549.html