算法:链表part01: 203.移除链表元素 + 707.设计链表 + 206.反转链表
链表理论(引用老师的啦)
203. 移除链表元素
题目:https://leetcode.cn/problems/remove-linked-list-elements/description/
讲解:https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html
思想
- 首先移除链表元素分两种情况:(1)单独处理头结点;(2)设置一个虚拟指针preHead指向head,统一处理所有节点,最后返回preHead.next;时间复杂度为O(n),空间复杂度为O(1);
- 也可以使用递归的方案,文章中有,简要概述为:找到递归终止条件,具体细节看讲解吧,重要的是递归思想难理解一些;
解题
- 单独处理头结点
/**
* 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) {
//单独处理头结点
while(head!=null&&head.val==val){
head=head.next;
}
if(head==null){
return head;
}
ListNode cur=head;
while(cur.next!=null){
if(cur.next.val==val){
cur.next=cur.next.next;
}else{
cur=cur.next;
}
}
return head;
}
}
//参考答案更好理解,上面是我写的
/**
* 方法1
* 时间复杂度 O(n)
* 空间复杂度 O(1)
* @param head
* @param val
* @return
*/
public ListNode removeElements(ListNode head, int val) {
while (head != null && head.val == val) {
head = head.next;
}
// 已经为null,提前退出
if (head == null) {
return head;
}
// 已确定当前head.val != val
ListNode pre = head;
ListNode cur = head.next;
while (cur != null) {
if (cur.val == val) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return head;
}
- 设置一个虚拟指针preHead指向head,统一处理所有节点
/**
* 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) {
//遍历链表,判断相同并删除
//统一对所有节点的操作
ListNode preHead=new ListNode();
preHead.next=head;
ListNode cur=preHead;
while(cur.next!=null){
if(cur.next.val==val){
cur.next=cur.next.next;
}else{
cur=cur.next;
}
}
return preHead.next;
}
}
//参考答案更好理解,上面是我写的
/**
* 时间复杂度 O(n)
* 空间复杂度 O(1)
* @param head
* @param val
* @return
*/
public ListNode removeElements(ListNode head, int val) {
// 设置一个虚拟的头结点
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;
}
}
return dummy.next;
}
707.设计链表
题目:https://leetcode.cn/problems/design-linked-list/description/
文章讲解:https://programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE%E8%A1%A8.html
思想
- 插入删除:设置虚拟指针处理头结点;在尾部插入删除时要单独操作;用while循环
- 找到下标index的位置:用for循环遍历到index
解题
class MyLinkedList {
//节点类
class ListNode{
int val;
ListNode next;
public ListNode(int val){
this.val=val;
}
}
//size存储链表长度
private int size=0;
//设置虚拟头节点,统一节点处理方法
private ListNode head;
public MyLinkedList() {
this.size=0;
this.head=new ListNode(0);
}
public int get(int index) {
if(index>=size||index<0){
return -1;
}
ListNode cur=head;
for(int i=0;i<=index;i++){
cur=cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
if(head==null){
return;
}
ListNode newNode=new ListNode(val);
newNode.next=head.next;
head.next=newNode;
this.size++;
}
public void addAtTail(int val) {
if(head==null){
return;
}
ListNode cur=head;
//循环找到链表尾
while(cur.next!=null){
cur=cur.next;
}
//找到链表尾,在尾部插入
ListNode newNode=new ListNode(val);
newNode.next=null;
cur.next=newNode;
this.size++;
}
public void addAtIndex(int index, int val) {
ListNode newNode=new ListNode(val);
ListNode cur=head;
if(index>size){
return;
}
if(index==size){
newNode.next=null;
cur.next=newNode;
this.size++;
return;
}
//找到对应下标的前一个位置
for(int i=0;i<index;i++){
cur=cur.next;
}
//插入新元素
newNode.next=cur.next;
cur.next=newNode;
this.size++;
}
public void deleteAtIndex(int index) {
if(index>=size||index<0){
return;
}
ListNode cur=head;
//找到要删除元素的前一个位置
for(int i=0;i<index;i++){
cur=cur.next;
}
if(cur.next.next==null){
cur.next=null;
this.size--;
return;
}else{
cur.next=cur.next.next;
this.size--;
return;
}
}
}
/**
* 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);
*/
//参考答案更好理解,上面是我写的
//单链表
class MyLinkedList {
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val=val;
}
}
//size存储链表元素的个数
private int size;
//注意这里记录的是虚拟头结点
private ListNode head;
//初始化链表
public MyLinkedList() {
this.size = 0;
this.head = new ListNode(0);
}
//获取第index个节点的数值,注意index是从0开始的,第0个节点就是虚拟头结点
public int get(int index) {
//如果index非法,返回-1
if (index < 0 || index >= size) {
return -1;
}
ListNode cur = head;
//第0个节点是虚拟头节点,所以查找第 index+1 个节点
for (int i = 0; i <= index; i++) {
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
ListNode newNode = new ListNode(val);
newNode.next = head.next;
head.next = newNode;
size++;
// 在链表最前面插入一个节点,等价于在第0个元素前添加
// addAtIndex(0, val);
}
public void addAtTail(int val) {
ListNode newNode = new ListNode(val);
ListNode cur = head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = newNode;
size++;
// 在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
// addAtIndex(size, val);
}
// 在第 index 个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
// 如果 index 等于链表的长度,则说明是新插入的节点为链表的尾结点
// 如果 index 大于链表的长度,则返回空
public void addAtIndex(int index, int val) {
if (index < 0 || index > size) {
return;
}
//找到要插入节点的前驱
ListNode pre = head;
for (int i = 0; i < index; i++) {
pre = pre.next;
}
ListNode newNode = new ListNode(val);
newNode.next = pre.next;
pre.next = newNode;
size++;
}
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}
//因为有虚拟头节点,所以不用对index=0的情况进行特殊处理
ListNode pre = head;
for (int i = 0; i < index ; i++) {
pre = pre.next;
}
pre.next = pre.next.next;
size--;
}
}
总结
是一道对链表进行基础操作的题目,用来构建链表类;
206.反转链表
题目:https://leetcode.cn/problems/reverse-linked-list/description/
文章讲解:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html
思想
206.反转链表
题目:https://leetcode.cn/problems/reverse-linked-list/description/
讲解:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html
思想
- 逻辑:关键在于定义一个pre和一个cur,让cur.next=pre;
- 边界条件:cur==null时便不需要再反转,所有边界条件是while(cur!=null)
解题
/**
* 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) {
ListNode pre=null;
ListNode cur=head;
while(cur!=null){
ListNode temp=cur.next;
cur.next=pre;
//后移
pre=cur;
cur=temp;
}
return pre;
}
}
////参考答案更好理解,上面是我写的
// 双指针
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode cur = head;
ListNode temp = null;
while (cur != null) {
temp = cur.next;// 保存下一个节点
cur.next = prev;
prev = cur;
cur = temp;
}
return prev;
}
}
总结
参照思想