首页 > 其他分享 >代码随想录-链表基础

代码随想录-链表基础

时间:2023-02-20 21:33:06浏览次数:45  
标签:index head ListNode cur val 代码 随想录 next 链表

链表

链表基础

单链表

代码随想录 (programmercarl.com)

package com.lee;

/**
 * @author Jun
 * @date 2023/2/17 21:53
 * @description ListNode
 */
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;
    }

    class MyLinkedList {
        // 存除链表元素的个数
        int size;
        // 虚拟头结点
        ListNode head;

        // 初始化链表
        public MyLinkedList(){
            size = 0;
            head = new ListNode();
        }

        // 获取第index个节点的数值,从0开始,第0个节点就是头结点
        public int get(int index) {
            // index非法,返回-1
            if (index < 0 || index >= size) {
                return -1;
            }
            ListNode currentNode = head;
            // 包含一个虚拟头节点,所以查找第index+1个节点
            for (int i = 0; i <= index; i++) {
                currentNode = currentNode.next;
            }
            return currentNode.val;
        }

        // 在链表最前面插入一个元素,等价于第0个元素前添加
        public void addAtHead(int val) {
            addAtIndex(0, val);
        }

        // 在链表最后一个节点插入一个元素
        public void addAtTail(int val) {
            addAtIndex(size, val);
        }

        /**
         * 在第index个节点之前插入一个新节点,如果为0则该节点为头结点
         * 如果index等于链表的长度,则新插入节点为链表尾节点
         * 如果index大于链表长度,则返回空
         */
        public void addAtIndex(int index, int val) {
            if (index > size) {
                return ;
            }
            if (index < 0) {
                index = 0;
            }
            // 添加元素所以长度加一
            size++;
            // 找到要插入节点的前驱
            ListNode pred = head;
            for (int i = 0; i < index; i++) {
                pred = pred.next;
            }
            ListNode toAdd = new ListNode(val);
            toAdd.next = pred.next;
            pred.next = toAdd;
        }

        // 删除第index个节点
        public void deleteAtIndex(int index) {
            if (index < 0 || index >= size) {
                return;
            }
            // 减少元素长度减一
            size--;
            if (index == 0) {
                head = head.next;
                return;
            }
            ListNode pred = head;
            for (int i = 0; i < index; i++) {
                pred = pred.next;
            }
            pred.next = pred.next.next;

        }
    }

    //打印输出方法
    static void print(ListNode listNoed){
        //创建链表节点
        while(listNoed!=null){
            System.out.println("节点:"+listNoed.val);
            listNoed=listNoed.next;
        }
    }

    public static void main(String[] args) {
        // 创建链表
        ListNode node = new ListNode(0);
        ListNode cur;
        cur = node;
        for (int i = 1; i < 5; i++) {
            ListNode one = new ListNode(i);
            cur.next = one;
            cur = cur.next;
        }
    }
}

移除链表元素

题意:删除链表中等于给定值 val 的所有节点。

示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]

设置虚拟头结点:return头结点的时候,需要return dummyNode.next,这才是新的头结点

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        // 如果头结点为空,直接返回空链表
        if (head == null) {
            return head;
        }
        // 设置虚拟头结点
        ListNode dummy = new ListNode(-1, head);
        // 设置前、后两个结点
        ListNode pre = dummy;
        ListNode cur = head;
        // 如果当前结点不为空的话就一直循环
        while (cur != null) {
            if (cur.val == val) {
                // 与目标值相同,则切换pre的next
                pre.next = cur.next;
            } else {
                // 不同的话,顺理成章前往后移
                pre = cur;
            }
            cur = cur.next;
        }
        return dummy.next;
    }
}

反转链表

代码随想录 (programmercarl.com)

题意:反转一个单链表。

示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
// 双指针
 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;
        }
        // 因为反转,最后返回prev
        return prev;
    }

标签:index,head,ListNode,cur,val,代码,随想录,next,链表
From: https://www.cnblogs.com/junun/p/17138993.html

相关文章