203. 移除链表元素
题目链接:https://leetcode.cn/problems/remove-linked-list-elements/description/
解题思路
代码如下:
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
// 虚拟头结点
ListNode rtn = new ListNode();
rtn.next = head;
head = rtn;
while(head.next != null) {
if (head.next.val == val) {
head.next = head.next.next;
} else {
head = head.next;
}
}
return rtn.next;
}
}
707. 设计链表
题目链接:https://leetcode.cn/problems/design-linked-list/
注意事项:每次操作链表节点时,都需要注意节点的prev、next属性处理以及first、last节点的变更判断。
/**
* 设计链表
*/
public class MyLinkedList {
MyNode first;
MyNode last;
int size;
public MyLinkedList() {
size = -1;
first = null;
last = null;
}
public int get(int index) {
if (index > size || index < 0 || first == null) {
return -1;
}
MyNode cur = getNode(index);
if (cur == null) {
return -1;
}
return cur.val;
}
private MyNode getNode(int index) {
if (index == 0) {
return first;
}
if (index == size) {
return last;
}
MyNode cur = first;
for (int i=1; i<=index; i++) {
if (cur.next == null) {
return null;
}
cur = cur.next;
}
return cur;
}
public void addAtHead(int val) {
first = new MyNode(val, first, null);
if (last == null) {
last = first;
} else {
// 需要将旧的头节点执行像一个节点
first.next.prev = first;
}
size++;
}
public void addAtTail(int val) {
MyNode newLast = new MyNode(val, null, last);
if (last == null) {
last = newLast;
first = newLast;
} else {
last.next = newLast;
last = newLast;
}
size++;
}
public void addAtIndex(int index, int val) {
if (index < 0 || index > size+1) {
return;
}
if (index == size+1) {
addAtTail(val);
return;
}
if (index == 0) {
addAtHead(val);
return;
}
MyNode cur = getNode(index);
MyNode last = cur.prev;
last.next = new MyNode(val, cur, last);
cur.prev = last.next;
size++;
}
public void deleteAtIndex(int index) {
if (index < 0 || index > size) {
return;
}
MyNode cur = getNode(index);
MyNode last = cur.prev;
// 如果上一个节点为空,则表示是首节点,则将下一个节点设置为头节点
if (last == null) {
first = first.next;
if (first == null) {
// 如果删除后,头结点为空,表示链表为空
this.last = null;
} else {
first.prev = null;
}
} else {
last.next = cur.next;
if (cur.next != null) {
cur.next.prev = last;
} else {
// 如果cur的下一个节点为空,表示为尾结点,需要将last设置为尾节点
this.last = last;
}
}
size--;
}
private static class MyNode{
int val;
MyNode next;
MyNode prev;
MyNode(int val, MyNode next ,MyNode prev){
this.val = val;
this.next = next;
this.prev = prev;
}
}
}
206. 反转链表
题目链接:https://leetcode.cn/problems/reverse-linked-list/description/
解题思路
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode last = null;
ListNode cur = head;
ListNode next = null;
while(cur != null) {
// 保存下一个节点
next = cur.next;
// 将当前节点指向上一个节点
cur.next = last;
// 跳转到下一个节点
last = cur;
cur = next;
}
return last;
}
}