力扣707题
单向链表
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class MyLinkedList {
int size;
ListNode head;
public MyLinkedList() {
size = 0;
head = new ListNode(0);
}
public int get(int index) {
//无效索引,返回-1
if(index < 0 || index >= size) {
return -1;
}
//寻找节点值
ListNode cur = head;
for(int i = 0; i < index + 1; i++) {
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
//相当于在索引为0的链表处添加了一个节点
addAtIndex(0, val);
}
public void addAtTail(int val) {
//相当于在索引为size的链表处添加了一个节点
addAtIndex(size, val);
}
public void addAtIndex(int index, int val) {
//添加节点位置 大于 链表索引
if(index > size) {
return;
}
//添加索引 小于 0, 那就给它算作是0
if(index < 0) {
index = 0;
}
size++;
//寻找要添加索引的前一个 节点
ListNode pre = head;
for(int i = 0; i < index; i++) {
pre = pre.next;
}
//添加节点
ListNode toAdd = new ListNode(val);
toAdd.next = pre.next;
pre.next = toAdd;
}
public void deleteAtIndex(int index) {
//无效索引
if(index < 0 || index >= size) {
return;
}
size--;
//寻找要删除节点的前一个结点
ListNode pre = head;
for(int i = 0; i < index; i++) {
pre = pre.next;
}
//删除操作
pre.next = pre.next.next;
}
}
双向链表
public class ListNode {
int val;
ListNode next;
ListNode pre;
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.pre = head;
}
public int get(int index) {
//无效索引,返回-1
if(index < 0 || index >= size) {
return -1;
}
//寻找节点值
ListNode cur = head;
if (index + 1 < size - index) {
for(int i = 0; i < index + 1; ++i) {
cur = cur.next;
}
} else {
cur = tail;
for(int i = 0; i < size - index; ++i) {
cur = cur.pre;
}
}
return cur.val;
}
public void addAtHead(int val) {
ListNode pre = head;
ListNode after = head.next;
size++;
//添加节点
ListNode toAdd = new ListNode(val);
toAdd.next = after;
toAdd.pre = pre;
pre.next = toAdd;
after.pre = toAdd;
}
public void addAtTail(int val) {
ListNode pre = tail.pre;
ListNode after = tail;
size++;
//添加节点
ListNode toAdd = new ListNode(val);
toAdd.next = after;
toAdd.pre = pre;
pre.next = toAdd;
after.pre = toAdd;
}
public void addAtIndex(int index, int val) {
//添加节点位置 大于 链表索引
if(index > size) {
return;
}
//添加索引 小于 0, 那就给它算作是0
if(index < 0) {
index = 0;
}
//寻找要添加索引的前一个节点 和 后一个节点
ListNode pre, after;
if(index < size - index) {
pre = head;
for(int i = 0; i < index; i++) {
pre = pre.next;
}
after = pre.next;
} else {
after = tail;
for(int i = 0; i < size - index; i++) {
after = after.pre;
}
pre = after.pre;
}
size++;
//添加节点
ListNode toAdd = new ListNode(val);
toAdd.next = after;
toAdd.pre = pre;
pre.next = toAdd;
after.pre = toAdd;
}
public void deleteAtIndex(int index) {
//无效索引
if(index < 0 || index >= size) {
return;
}
//寻找要删除索引的前一个节点 和 后一个节点
ListNode pre, after;
if(index < size - index) {
pre = head;
for(int i = 0; i < index; i++) {
pre = pre.next;
}
after = pre.next.next;
} else {
after = tail;
//size - index - 1为啥?你得找到所要删除节点的前一个节点的索引,那必须减一
for(int i = 0; i < size - index - 1; i++) {
after = after.pre;
}
pre = after.pre.pre;
}
size--;
//删除操作
pre.next = after;
after.pre = pre;
}
}
/**
* 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);
*/
双向链表也可以不用这么写,不考虑是从前还是从后开始查,这样会更简单一些,哈哈!那这样的话,代码比我这样写要简单很多了,只需要考虑从前往后查就可以!