数据结构:链表,leetcode203题删除链表元素
链表:真正的动态数据结构。 1最简单的动态数据结构。2也能帮助更深入的理解引用和指针。具有递归结构性质
数据存储在Node节点中, E存储元素,Next代表下一个元素节点。最后一个元素为NULL。 就像或者车厢之间链接一样 next负责链接。
优点:就是真正的动态,不需要处理固定容量的问题。
缺点:丧失了随机访问的能力。
对链表的理解:其实就是创建了一个又一个对象然后每个对象都存储着后一位对象;所以它只能往后循环。不能倒着循环。
/** * 链表结构 只能从前往后查。从头节点动态加入。 **/ public class LinkedList<E> { private class Node{ public E e ; //本节点存储的数据 public Node next; public Node(E e, Node next){ this.e = e; this.next = next; } public Node(E e){ this(e,null); } public Node(){ this(null,null); } @Override public String toString(){ return e.toString(); } } private Node dummyHead; //虚拟头节点 private int size; //链表的长度 public LinkedList(){ dummyHead = new Node(null,null); size = 0; } /** 从头节点开始添加 是O(1) 新加入的元素的next 给head 然后再把head指向新加入的元素。即是新加入的成为了新的头节点。 **/ public void add(int index, E e){ if(index < 0 || index > size) throw new IllegalArgumentException("Add failed. Illegal index. "); Node prev = dummyHead; for(int i = 0; i< index ; i++){ prev = prev.next; } prev.next = new Node(e, prev.next); size ++; } public void addFirst(E e){ add(0,e); } public void addLast(E e){add(size,e);} public int getSize(){return size; } public boolean isEmpty(){return size == 0;} public E remove (int index){ if(index < 0 || index >= size) throw new IllegalArgumentException("remove failed. Illegal index."); Node prev = dummyHead; for(int i = 0; i< index ; i++){ prev = prev.next; } Node delNode = prev.next; prev.next = delNode.next; delNode.next = null; size --; return delNode.e; } public E removeFirst (){return remove(0);}; public E removeLast (){return remove(size-1);}; public E get(int index ){ if(index < 0 || index >= size) throw new IllegalArgumentException("Get failed. Illegal index."); Node cur = dummyHead.next; for(int i = 0; i< index ; i++){ cur = cur.next; } return cur.e; } public E getFirst(){ return get(0); } public E getLast(){ return get(size -1); } //修改方法 public void set(int index,E e){ if(index < 0 || index >= size) throw new IllegalArgumentException("Update failed. Illegal index."); Node cur = dummyHead.next; //从1开始 for(int i = 0; i< index ; i++){ cur = cur.next; } cur.e = e ; } //是否包含方法 public boolean contains(E e){ Node cur = dummyHead.next; while(cur.next != null){ if(cur.equals(e)) return true; cur = cur.next; } return false; } @Override public String toString(){ StringBuilder res = new StringBuilder(); for(Node cur = dummyHead.next ; cur != null ; cur = cur.next) res.append(cur + "->"); res.append("NULL"); res.append("NULL"); return res.toString(); } public static void main(String[] args) { LinkedList<Integer> linkedList = new LinkedList(); for (int i = 0; i< 5 ; i++) { linkedList.addFirst(i); System.out.println(linkedList); } linkedList.add(3,666); System.out.println(linkedList); linkedList.remove(3); System.out.println(linkedList); linkedList.removeFirst(); System.out.println(linkedList); linkedList.removeLast(); System.out.println(linkedList); } }
用链表解决leetcode203题 删除链表元素 https://leetcode-cn.com/problems/remove-linked-list-elements/ 虚拟头节点默认都是null 如果后面有数据 next 就不是null了
public class ListNode { int val; ListNode next; public ListNode(int x ){ val = x;} } public class Solution { public ListNode removeElement(ListNode head ,int val){ while(head != null && head.val == val){ ListNode delNode = head; head = head.next; delNode = null; // head = head.next; } if(head == null) return null; ListNode prev = head; while(prev.next != null){ if(prev.next.val == val) { ListNode delNode = prev.next; prev.next = delNode.next; delNode.next = null; // prev.next = prev.next.next }else{ prev = prev.next; } } return head; } } public class Solution2 { public ListNode removeElement(ListNode head ,int val){ ListNode dummyHead = new ListNode(-1); dummyHead.next = head; ListNode prev = dummyHead; while(prev.next != null){ if(prev.next.val == val) { prev.next = prev.next.next; }else{ prev = prev.next; } } return dummyHead.next; } }
相关推荐
koushr 2020-11-12
范范 2020-10-28
qiangde 2020-09-13
范范 2020-07-30
mingyunxiaohai 2020-07-19
kka 2020-09-14
成长共勉 2020-05-19
ipqtjmqj 2020-05-19
zhaochen00 2020-10-13
Mars的自语 2020-09-27
steeven 2020-09-18
聚沙成塔积水成渊 2020-08-16
earthhouge 2020-08-15
aanndd 2020-08-12
bluetears 2020-07-28
horizonheart 2020-07-19
liushall 2020-07-18
bluetears 2020-07-05
fengshantao 2020-07-02