1 public class Solution
  2 {
  3   public static void main(String[] args)
  4   {
  5     // 1 --> 2 --> 3 --> 5 --> 4 --> 6 --> 0 --> null
  6     // 0     1     2     3     4     5     6
  7     MyLinkedList l = new MyLinkedList();
  8 
  9     l.addHead(5);
 10     l.addHead(3);
 11     l.addHead(2);
 12     l.addHead(1);
 13     l.addTail(4);
 14     l.addTail(6);
 15     l.addTail(0);
 16 
 17     ListNode head = l.head;
 18     while (head != null) {
 19       System.out.print(head.value + " -> ");
 20       head = head.next;
 21     }
 22 
 23     System.out.println();
 24     System.out.print(l.get(1) + " , ");
 25     System.out.print(l.get(0) + " , ");
 26     System.out.print(l.get(5) + " , ");
 27     System.out.print(l.get(6) + " , ");
 28 
 29     System.out.println();
 30 
 31     l.remove(2);
 32     l.remove(5);
 33     head = l.head;
 34     while (head != null) {
 35       System.out.print(head.value + " -> ");
 36       head = head.next;
 37     }
 38 
 39     System.out.println();
 40     l.removeVal(4);
 41     l.removeVal(5);
 42 
 43     head = l.head;
 44     while (head != null) {
 45       System.out.print(head.value + " -> ");
 46       head = head.next;
 47     }
 48   }
 49 }
 50 
 51 
 52 public class ListNode {
 53     int value;
 54     ListNode next;
 55     // ListNode prev;
 56     public ListNode(int value) {
 57         this.value = value;
 58     }
 59 }
 60 
 61 public class MyLinkedList {
 62     public ListNode head;
 63     public ListNode tail;
 64     public int length;
 65     
 66     public MyLinkedList() {
 67         head = null;
 68         tail = null;
 69         length = 0;
 70     }
 71 
 72     public void addHead(int value) {
 73         ListNode newHead = new ListNode(value);
 74         newHead.next = head;
 75         head = newHead;
 76         if (length == 0) {  // records previous node number
 77             tail = head;
 78         }
 79         length++;
 80     }
 81     
 82     public void addTail(int value) {
 83         ListNode newTail = new ListNode(value);
 84      if (length == 0) {
 85           head = newTail;
 86      } else {
 87        tail.next = newTail; 
          // newTail.prev = tail;
88      } 89     tail = newTail; 90     length++; 91 } 92 93 public Integer get(int index) { // Zero based 94 if (index < 0 || index >= length) { 95 return null; 96 } 97 ListNode cur = head; 98 // 1 → 2 → 3 → 4 → 99 // 0 1 100      // 0 1 2 101      for (int i = 0; i < index; i++) { 102 cur = cur.next; 103 } 104 return cur.value; 105 } 106 107 public void remove(int index) { 108 if (index < 0 || index >= length) { 109 return; 110 } 111 // 1 → 2 → 3 → 4 → 112 // 0 1 113      // 0 1 2 114      if (index == 0) { 115      if (length == 1) { // It cannot be length = 0, because index = length has been filtered out! 116      tail = null; 117      } 118      head = head.next; 119      } else { 120        ListNode prev = head; 121      for (int i = 0; i < index - 1; i++) { 122     prev = prev.next; 123      } 124   //What if index is the position of tail node? 125     if (prev.next == tail) { // ← Or index == length - 1 126     tail = prev; 127     } 128     prev.next = prev.next.next; 129     } 130     length--; 131 } 132 133 134 //Remove first value val, not index 135 public ListNode removeVal(int val) { 136 if (head == null) { 137 return null; 138 } 139 ListNode cur = head; 140 if (head.value == val) { 141 head = head.next; 142 length--; 143 return cur; 144 } 145 146 ListNode prev = null; 147 while (cur != null) { 148 if (cur.value == val) { 149 prev.next = prev.next.next; 150 cur.next = null; 151 length--; 152 return cur; 153 } 154 prev = cur; 155 cur = cur.next; 156 } 157 return null; 158 } 159 160 public int size() { 161 return length; 162 } 163 }

 

posted on 2018-03-20 19:27  mayinmiao  阅读(119)  评论(0编辑  收藏  举报