707、设计链表
具体实现:
1.使用单向链表
删除链表节点:
添加链表节点:
2.使用双向链表
删除链表节点:
添加链表节点:
代码:
单向链表
class ListNode{//单向链表 int val; ListNode next; ListNode(){} ListNode(int val){ this.val = val; } } class MyLinkedList { int size;//存储链表元素的个数 ListNode head;//虚拟头结点 public MyLinkedList() {//初始化链表 size = 0; head = new ListNode(0); } public int get(int index) {//获取第index个节点的数值 if (index < 0 || index >= size){ return -1; } ListNode currentNode = head; for (int i = 0; i <= index; i++){ currentNode = currentNode.next; } return currentNode.val; } public void addAtHead(int val) {//在链表最前面插入一个节点 addAtIndex(0, val); } public void addAtTail(int val) {//在链表的最后插入节点 addAtIndex(size, val); } public void addAtIndex(int index, int val) { if (index > size){ return; } if (index < 0){ index = 0; } size++;//插入一个节点链表变长 ListNode pred = head; for (int i = 0; i < index; i++){ pred = pred.next;//找到要插入节点的前驱 } ListNode toAdd = new ListNode(val); toAdd.next = pred.next; pred.next = toAdd; } public void deleteAtIndex(int index) { if (index < 0 || index >= size){ return; } size--; ListNode pred = head; for (int i = 0; i < index; i++){ pred = pred.next; } pred.next = pred.next.next; } } /** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList obj = new MyLinkedList(); * int param_1 = obj.get(index); * obj.addAtHead(val); * obj.addAtTail(val); * obj.addAtIndex(index,val); * obj.deleteAtIndex(index); */
2.双向链表
class ListNode{//双向链表 int val; ListNode next, prev; ListNode(int x){val = x;} } class MyLinkedList { int size;//存储链表元素的个数 ListNode head;//虚拟头结点 ListNode tail; public MyLinkedList() {//初始化链表 size = 0; head = new ListNode(0); tail = new ListNode(0); head.next = tail; tail.prev = head; } public int get(int index) {//获取第index个节点的数值 if (index < 0 || index >= size){ return -1; } ListNode currentNode = head; if (index < (size -1)/2){ // 通过判断 index < (size - 1) / 2 来决定是从头结点还是尾节点遍历,提高效率 for (int i = 0; i <= index; i++){ currentNode = currentNode.next; } }else { currentNode = tail; for (int i = 0; i <= size - index - 1; i++){ currentNode = currentNode.prev; } } return currentNode.val; } public void addAtHead(int val) {//在链表最前面插入一个节点 addAtIndex(0, val); } public void addAtTail(int val) {//在链表的最后插入节点 addAtIndex(size, val); } public void addAtIndex(int index, int val) { if (index > size){ return; } if (index < 0){ index = 0; } size++;//插入一个节点链表变长 ListNode pred = head; for (int i = 0; i < index; i++){ pred = pred.next;//找到要插入节点的前驱 } ListNode toAdd = new ListNode(val); toAdd.next = pred.next; pred.next.prev = toAdd; toAdd.prev = pred; pred.next = toAdd; } public void deleteAtIndex(int index) { if (index < 0 || index >= size){ return; } size--; ListNode pred = head; for (int i = 0; i < index; i++){ pred = pred.next; } pred.next.next.prev = pred; pred.next = pred.next.next; } } /** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList obj = new MyLinkedList(); * int param_1 = obj.get(index); * obj.addAtHead(val); * obj.addAtTail(val); * obj.addAtIndex(index,val); * obj.deleteAtIndex(index); */