1. 移除链表元素
203. 移除链表元素https://leetcode.cn/problems/remove-linked-list-elements/description/
给你一个链表的头节点 head
和一个整数 val
,请你删除链表中所有满足 Node.val == val
的节点,并返回新的头节点。
示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
示例 3:
输入:head = [7,7,7,7], val = 7
输出:[]
解题思路
双指针,pre指向上一个,cur指向当前,遇到为val的就让pre.next指向cur.next。
边界值分析:
沿着链表下去第一个不为null的节点为新head,终止状态是节点为null或者值不为val(循环条件是不为null且为val)。找到新head后,进行双指针遍历,终止条件是cur为null。
代码
class Solution {
public ListNode removeElements(ListNode head, int val) {
while (head != null && head.val == val) {
head = head.next;
}
if (head == null) {
return head;
}
ListNode pre = head, cur = head.next;
while(cur!=null) {
if(cur.val==val) {
pre.next = cur.next;
}else {
pre = cur;
}
cur= pre.next;
}
return head;
}
}
2. 设计链表
707. 设计链表https://leetcode.cn/problems/design-linked-list/description/
你可以选择使用单链表或者双链表,设计并实现自己的链表。
单链表中的节点应该具备两个属性:val
和 next
。val
是当前节点的值,next
是指向下一个节点的指针/引用。
如果是双向链表,则还需要属性 prev
以指示链表中的上一个节点。假设链表中的所有节点下标从 0 开始。
实现MylinkedList 类:
- MylinkedList() 初始化 MylinkedList 对象。
- int get(int index) 获取链表中下标为 index 的节点的值。如果下标无效,则返回
-1
。 - void addAtHead(int val) 将一个值为 val 的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。
- void addAtTail(int val) 将一个值为 val的节点追加到链表中作为链表的最后一个元素。
- void addAtIndex(int index, int val) 将一个值为 val的节点插入到链表中下标为 index 的节点之前。如果 index 等于链表的长度,那么该节点会被追加到链表的末尾。如果
index
比长度更大,该节点将不会插入到链表中。 - deleteAtIndex(int index) 如果下标有效,则删除链表中下标为 index的节点。
解题思路
题目要求没有涉及到需要双链表的地方,所以直接使用单链表。
需要先定义一个内部Node类,类包含一个int的val,一个next指针。无参有参构造。
MyLinkedList涉及到了index的索引,所以加一个size属性用于index范围的判断,避免遍历判断。
代码
class MyLinkedList {
class LinkedNode {
int val;
LinkedNode next;
public LinkedNode() {
}
public LinkedNode(int val) {
this.val = val;
}
}
private LinkedNode head;
private int size;
public MyLinkedList() {
size = 0;
}
public MyLinkedList(int val) {
size = 1;
this.head = new LinkedNode(val);
}
public int get(int index) {
if (index < 0 || index > size - 1) {
return -1;
}
LinkedNode cur = this.head;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
LinkedNode cur = new LinkedNode(val);
cur.next = this.head;
this.head = cur;
this.size++;
}
public void addAtTail(int val) {
if (this.size == 0) {
this.head = new LinkedNode(val);
} else {
LinkedNode cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = new LinkedNode(val);
}
this.size++;
}
public void addAtIndex(int index, int val) {
if (index < 0 || index > size)
return;
if (index == 0)
this.addAtHead(val);
else if (index == size)
this.addAtTail(val);
else {
LinkedNode cur = this.head;
for (int i = 0; i < index - 1; i++) {
cur = cur.next;
}
LinkedNode young = new LinkedNode(val);
young.next = cur.next;
cur.next = young;
this.size++;
}
}
public void deleteAtIndex(int index) {
if (index < 0 || index > size - 1)
return;
if (index == 0) {
this.head = this.head.next;
} else {
LinkedNode cur = this.head;
for (int i = 0; i < index - 1; i++) {
cur = cur.next;
}
LinkedNode temp = cur.next;
cur.next = temp.next;
temp.next = null;
}
this.size--;
}
}
3. 反转链表
206. 反转链表https://leetcode.cn/problems/reverse-linked-list/description/
给你单链表的头节点 head
,请你反转链表,并返回反转后的链表。
示例 1:
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
示例 2:
输入:head = [1,2]
输出:[2,1]
示例 3:
输入:head = []
输出:[]
解题思路
双指针进行遍历,pre,cur,用一个temp保留next。temp保存cur的next节点,cur反向指向pre,pre前移到cur,cur前移到temp。
边界值分析:
head为null或者head.next为null的时候直接返回head。终止条件是cur为null的时候,返回pre就是新head。
代码
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode pre = head;
ListNode cur = head.next;
pre.next = null;
ListNode temp = null;
while (cur != null) {
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
4. 两两交换链表中的节点
24. 两两交换链表中的节点https://leetcode.cn/problems/swap-nodes-in-pairs/description/给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
示例 1:
输入:head = [1,2,3,4]
输出:[2,1,4,3]
示例 2:
输入:head = []
输出:[]
示例 3:
输入:head = [1]
输出:[1]
解题思路
还是使用双指针,pre指向前一个节点,temp保留下一个节点,中间的两个节点的头节点为cur。cur和cur的next先进行逆序转换,pre的next指向cur,cur的next的next指向temp。
边界值分析:
如果head为null或者head.next为null就直接返回head。当cur为null的时候为终止条件。
代码
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode pre = head;
ListNode cur = head.next.next;
head = this.swap(head);
ListNode temp = null;
while (cur != null) {
temp = cur.next != null ? cur.next.next : null;
cur = swap(cur);
pre.next = cur;
pre = cur.next != null ? cur.next : cur;
cur = temp;
}
return head;
}
public ListNode swap(ListNode head) {
if (head != null && head.next != null) {
ListNode temp = head.next;
temp.next = head;
head.next = null;
head = temp;
}
return head;
}
}