力扣203:移除链表元素
题目链接:力扣203:移除链表元素
思路:
1.设置一个虚拟头节点dummy指向链表
2.设置一个指针cur指向头节点head
3.设置一个指针pre指向虚拟头节点dummy
4.cur依次遍历每个节点用于判断每个节点的值与目标值是否相同,pre则跟随在cur后面,用于做链表指针指向下一个节点的处理
代码:
/**
* 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 removeElements(ListNode head, int val) {
// 1.校验链表
if (head == null) {
return null;
}
// 2.设置虚拟头节点并指向head
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
// 3.设置当前节点指向head,并遍历所有节点
ListNode cur = head;
while (cur != null) {
if (cur.val == val) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return dummy.next;
}
}
力扣707:设计链表
题目链接:力扣707:设计链表
思路:
1.先定义两个全局变量,分别为链表长度size和虚拟头节点head = new ListNode(-1)
2.画出链表,根据指针的移动来进行增删查,要注意边界情况
代码:
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) {
this.val=val;
}
}
class MyLinkedList {
// 定义链表初始化长度
int size;
// 定义虚拟头节点
ListNode head;
public MyLinkedList() {
size = 0;
head = new ListNode(-1);
}
public int get(int index) {
// 校验参数
if (index < 0 || index >= size) {
return -1;
}
ListNode cur = head;
// 依次遍历每个节点
for (int i = 0; i <= index; i++) {
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
addAtIndex(0, val);
}
public void addAtTail(int val) {
addAtIndex(size, val);
}
public void addAtIndex(int index, int val) {
if (index > size) {
return;
}
if (index < 0) {
index = 0;
}
size++;
ListNode cur = head;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
ListNode newNode = new ListNode(val);
newNode.next = cur.next;
cur.next = newNode;
}
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}
size--;
if (index == 0) {
head = head.next;
return;
}
ListNode cur = head;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
ListNode temp = cur.next.next;
cur.next = temp;
}
}
/**
* 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);
*/
力扣206:反转链表
题目链接:力扣206:反转链表
思路:
一、双指针法
1.定义一个指针cur指向头节点,并用于遍历链表中的所有节点
2.定义另一个指针pre指向虚拟头节点(也就是反转后的最后一个节点,值为null),初始值为null
3.循环遍历cur指针,直到cur为null
4.提前保存cur.next并放到temp指针,将cur.next指向pre
5.将pre移动到cur位置,然后将cur移动到提前保存好的temp指针位置
二、递归法
可参考双指针法,将双指针每一次遍历节点的过程理解为每一次递归调用的过程
代码:
/**
* 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) {
// 1.定义指针指向head,用于遍历所有节点
ListNode cur = head;
// 2.定义指针指向虚拟头节点(也就是反转后的尾节点,值为null)
ListNode pre = null;
ListNode temp = null;
while (cur != null) {
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
// 递归法
class Solution {
public ListNode reverseList(ListNode head) {
// 1.定义指针指向head,用于遍历所有节点
ListNode cur = head;
// 2.定义指针指向虚拟头节点(也就是反转后的尾节点,值为null)
ListNode pre = null;
ListNode temp = null;
return reverse(pre, cur);
}
private ListNode reverse(ListNode pre, ListNode cur) {
if (cur == null) {
return pre;
}
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
return reverse(pre, cur);
}
}