链表的定义
public class ListNode {
//结点的值
int val;
//下一个结点
ListNode next;
//结点的构造函数(无参)
public ListNode() {
}
//结点的构造函数(有一个参数)
public ListNode(int val) {
this.val = val;
}
//结点的构造函数(有两个参数)
public ListNode(int val,ListNode next) {
this.val = val;
this.next = next;
}
}
LeetCode 203-移除链表元素
题目链接:
https://leetcode.cn/problems/remove-linked-list-elements/
解题思路:
-
不用虚拟头结点
首先判断head是否为空 再写head.val == val的情况,直接把下一个结点赋值给head 再继续循环判断
定义一个临时指针cur,将head赋值给cur,再判断cur和cur.next是否为空。head已经判断过值,直接判断cur.next.val 如果 == val就删除该结点
-
用虚拟头结点
定义一个dummy结点,dummy.next = head。定义临时指针cur,cur = dummy,从而使包括head在内的所有指针用相同的删除方式
代码实现
//漏洞百出的自己写的
class Solution {
public ListNode removeElements(ListNode head, int val) {
//没考虑head == null 和 head.val == val 的情况
if(head == null) {
return head;
}
while (head != null && head.val == val) {
head = head.next;
}
ListNode cur = head;
while (cur != null && cur.next != null) {//没考虑全 cur和cur.next都不能为空,只考虑了cur.next不为空
if (cur.next.val == val) {
cur.next = next.next.next;
}else {
cur = cur.next;
}
}
return head;
}
}
//示例 不用虚拟头节点
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
while (head != null && head.val == val) {
head = head.next;
}
ListNode cur = head;
while (cur != null && cur.next != null) {//防止操作空指针
if (cur.next.val == val) {
cur.next = cur.next.next;
}else {
cur = cur.next;
}
}
return head;
}
}
--------------------------------------------------------------------------------------
//虚拟节点示例
class Solution {
public ListNode removeElements(ListNode head, int val) {
//没判断head == null的情况
if (head == null) {
return head;
}
ListNode dummy = new ListNode();//忘了实例化虚拟指针
dummy.next = head;
ListNode cur = dummy;
while (cur.next != null) {
if (cur.next.val == val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
head = dummy.next;
return head;
}
}
难点:
- 忘记判断head为空的情况
无虚拟节点:
-
忘记判断head.val == val 的情况
-
line7:用while而不用if 有可能连续几个head.val == val 所以要持续判断,持续移除
-
因为是单向链表,如果把cur = head.next 则无法将 要删除元素的后一个结点 赋值给 head的前一个结点的next域
-
line13:没有判断cur和cur.next是否都不为空,即使判断cur.next不为空也无法保证cur一定不为空
有虚拟节点
-
创建一个结点需要实例化
-
为什么不用cur = dummy.next 理由同上3
-
head = dummy.next;
return head; 因为原来的head可能被删了,所以要把dummy.next作为链表的新头结点
LeetCode 203-移除链表元素
题目链接:
https://leetcode.cn/problems/design-linked-list/
代码实现
class MyLinkedList {
int size;
ListNode dum;
public MyLinkedList() {
size = 0;
dum = new ListNode(0);
}
//获取链表中第index个结点的值
public int get(int index) {
//size包括dum结点,如果index>=size-1 则越界
if (index < 0 || index > (size - 1)) {
return -1;
}
ListNode cur = dum.next;
//?dum.next:
//head结点(链表头结点)为第0个结点,size - 1 为最后一个结点
while (index != 0) {
cur = cur.next;
index--;
}
return cur.val;
}
//头部插入
public void addAtHead(int val) {
ListNode newNode = new ListNode(val);
//注意赋值顺序
newNode.next = dum.next;
dum.next = newNode;
size++;
}
//尾部插入
public void addAtTail(int val) {
ListNode newNode = new ListNode(val);
ListNode cur = dum;
while (cur.next != null) {
cur = cur.next;
}
cur.next = newNode;
size++;
}
//第n个结点前插入结点
//第n个结点看作cur.next?
//因为在第n个结点前插入结点,需要知道第n个结点之前的节点地址才能操作
public void addAtIndex(int index, int val) {
if (index > size || index < 0) {
return;
}
ListNode newNode = new ListNode(val);
ListNode cur = dum;
while (index != 0) {
cur = cur.next;
index--;
}
newNode.next = cur.next;
cur.next = newNode;
size++;
}
public void deleteAtIndex(int index) {
if (index >= size || index < 0) {
return;
}
ListNode cur = dum;
while (index != 0) {
cur = cur.next;
index--;
}
cur.next = cur.next.next;
size--;
}
}
难点:
- 忘记判断index的合法性
- 插入和删除,size需要对应地增加和删除
- 边界判断容易混乱
- 增加节点操作顺序为从后往前
LeetCode 206-反转链表
题目链接:
https://leetcode.cn/problems/reverse-linked-list/submissions/
解题思路:
用双指针改变结点指向的方向
代码实现
class Solution {
public ListNode reverseList(ListNode head) {
ListNode cur = head;
ListNode pre = null;
while (cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
难点:
- cur 和 pre 的赋值操作
- while的判断条件:到了最后不需要让null指向前一个结点,所以当cur == null时,pre变成头结点,跳出循环
- 用temp临时指针的原因:赋值前要将cur.next 保存在temp中,否则如果直接 cur.next = pre,则找不到原来的cur.next
- 一定要先pre = cur;再cur = temp的原因:如果先cur = temp,则找不到原来的cur的位置,pre无法再移动到cur原来的位置上