zoukankan      html  css  js  c++  java
  • 链表总结

    链表

    数据格式:一般是一个node,node中包含当前节点的值和指向下一个节点的指针
    常见的链表:单链,双链
    内存中存储方式:不像数组那般需要整块的空间,依靠指针寻找一下节点,所以足够大小的零散空间即可
    常见算法中的操作,翻转,删除某一节点,一般都可以通过虚拟头结点处理,或者单纯的操作链表

     1package ch2;
    2
    3public class DetectCycle {
    4
    5    private static class ListNode {
    6        int val;
    7        ListNode next;
    8
    9        ListNode() {
    10        }
    11
    12        ListNode(int val) {
    13            this.val = val;
    14        }
    15
    16        ListNode(int val, ListNode next) {
    17            this.val = val;
    18            this.next = next;
    19        }
    20
    21
    22    }
    23
    24    /**
    25     * 142. 环形链表 II
    26     给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
    27
    28     为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。
    29
    30     说明:不允许修改给定的链表。
    31
    32     进阶:
    33
    34     你是否可以使用 O(1) 空间解决此题?
    35
    36
    37     示例 1:
    38
    39
    40
    41     输入:head = [3,2,0,-4], pos = 1
    42     输出:返回索引为 1 的链表节点
    43     解释:链表中有一个环,其尾部连接到第二个节点。
    44     示例 2:
    45
    46
    47
    48     输入:head = [1,2], pos = 0
    49     输出:返回索引为 0 的链表节点
    50     解释:链表中有一个环,其尾部连接到第一个节点。
    51     示例 3:
    52
    53
    54
    55     输入:head = [1], pos = -1
    56     输出:返回 null
    57     解释:链表中没有环。
    58
    59
    60     提示:
    61
    62     链表中节点的数目范围在范围 [0, 104] 内
    63     -105 <= Node.val <= 105
    64     pos 的值为 -1 或者链表中的一个有效索引
    65     * @param head
    66     * @return
    67     */

    68    public ListNode detectCycle(ListNode head) {
    69        /**
    70         *
    71         * 快慢指针
    72         * 如果有环则必然相遇,相遇的位置就是头结点,返回环的入口节点,这个需要总结公式
    73         * 如果没有环,则是遍历了所有节点依旧没有相遇
    74         */

    75        ListNode fast = head;
    76        ListNode slow = head;
    77        while(fast!=null && fast.next!=null){
    78            fast = fast.next.next;
    79            slow = slow.next;
    80            if(slow == fast){
    81                ListNode index1 = fast;
    82                ListNode index2 =head;
    83                while(index1!=index2){
    84                    index1 = index1.next;
    85                    index2 = index2.next;
    86                }
    87                return index2;
    88            }
    89        }
    90        return null;
    91    }
    92}
      1package ch2;
    2
    3/**
    4 * 设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
    5 * <p>
    6 * 在链表类中实现这些功能:
    7 * <p>
    8 * get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
    9 * addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
    10 * addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
    11 * addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
    12 * deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
    13 *  
    14 * <p>
    15 * 示例:
    16 * <p>
    17 * MyLinkedList linkedList = new MyLinkedList();
    18 * linkedList.addAtHead(1);
    19 * linkedList.addAtTail(3);
    20 * linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
    21 * linkedList.get(1);            //返回2
    22 * linkedList.deleteAtIndex(1);  //现在链表是1-> 3
    23 * linkedList.get(1);            //返回3
    24 *  
    25 * <p>
    26 * 提示:
    27 * <p>
    28 * 所有val值都在 [1, 1000] 之内。
    29 * 操作次数将在  [1, 1000] 之内。
    30 * 请不要使用内置的 LinkedList 库。
    31 * <p>
    32 * 来源:力扣(LeetCode)
    33 * 链接:https://leetcode-cn.com/problems/design-linked-list
    34 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    35 */

    36public class MyLinkedList {
    37
    38    private static class ListNode {
    39        int val;
    40        ListNode next;
    41
    42        ListNode() {
    43        }
    44
    45        ListNode(int val) {
    46            this.val = val;
    47        }
    48
    49        ListNode(int val, ListNode next) {
    50            this.val = val;
    51            this.next = next;
    52        }
    53
    54
    55    }
    56
    57    int size;
    58    ListNode sentinel;
    59
    60    /**
    61     * Initialize your data structure here.
    62     */

    63    public MyLinkedList() {
    64        sentinel = new ListNode();
    65        size = 0;
    66    }
    67
    68    /**
    69     * Get the value of the index-th node in the linked list. If the index is invalid, return -1.
    70     * * get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
    71     */

    72    public int get(int index) {
    73        if (index >= size || index < 0) {
    74            return -1;
    75        }
    76        ListNode curr = sentinel.next;
    77        while ((index--) > 0) {
    78            curr = curr.next;
    79        }
    80        return curr.val;
    81    }
    82
    83    /**
    84     * Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
    85     * * addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
    86     */

    87    public void addAtHead(int val) {
    88        ListNode head = new ListNode(val);
    89        head.next = sentinel.next;
    90        sentinel.next = head;
    91        size++;
    92
    93    }
    94
    95    /**
    96     * Append a node of value val to the last element of the linked list.
    97     * * addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
    98     */

    99    public void addAtTail(int val) {
    100        ListNode newNode = new ListNode(val);
    101        ListNode curr = sentinel;
    102        while (curr.next != null) {
    103            curr = curr.next;
    104        }
    105        curr.next = newNode;
    106        size++;
    107    }
    108
    109    /**
    110     * Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
    111     * * addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
    112     */

    113    public void addAtIndex(int index, int val) {
    114        if (index > size) {
    115            return;
    116        }
    117
    118        ListNode newNode = new ListNode(val);
    119        ListNode curr = sentinel;
    120        while ((index--) > 0) {
    121            curr = curr.next;
    122        }
    123        newNode.next = curr.next;
    124        curr.next = newNode;
    125        size++;
    126
    127    }
    128
    129    /**
    130     * Delete the index-th node in the linked list, if the index is valid.
    131     * * deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
    132     */

    133    public void deleteAtIndex(int index) {
    134        if (index >= size || index < 0) {
    135            return;
    136        }
    137
    138        ListNode curr = sentinel;
    139        while ((index--) > 0) {
    140            curr = curr.next;
    141        }
    142        curr.next = curr.next.next;
    143        size--;
    144    }
    145
    146    public static void main(String[] args) {
    147        MyLinkedList obj = new MyLinkedList();
    148        obj.addAtHead(1);
    149        obj.addAtTail(3);
    150        obj.addAtIndex(12);
    151        int param_1 = obj.get(1);
    152        obj.deleteAtIndex(1);
    153        int param_2 = obj.get(1);
    154
    155    }
    156
    157}
     1package ch2;
    2
    3public class RemoveElements {
    4
    5    private static class ListNode {
    6        int val;
    7        ListNode next;
    8
    9        ListNode() {
    10        }
    11
    12        ListNode(int val) {
    13            this.val = val;
    14        }
    15
    16        ListNode(int val, ListNode next) {
    17            this.val = val;
    18            this.next = next;
    19        }
    20
    21
    22    }
    23
    24    /**
    25     * 203. 移除链表元素
    26     * 删除链表中等于给定值 val 的所有节点。
    27     * <p>
    28     * 示例:
    29     * <p>
    30     * 输入: 1->2->6->3->4->5->6, val = 6
    31     * 输出: 1->2->3->4->5
    32     *
    33     * @param head
    34     * @param val
    35     * @return
    36     */

    37    public static ListNode removeElements(ListNode head, int val) {
    38        ListNode sentinel = new ListNode(0,head);
    39        ListNode prev = sentinel,curr = head;
    40        while(curr!=null){
    41            if(curr.val==val){
    42                prev.next = curr.next;
    43            }else{
    44                prev =curr;
    45            }
    46            curr=curr.next;
    47        }
    48        return sentinel.next;
    49    }
    50
    51    public static void main(String[] args) {
    52        ListNode l4 = new ListNode(4);
    53        ListNode l3 = new ListNode(2,l4);
    54        ListNode l2 = new ListNode(4,l3);
    55        ListNode l1 = new ListNode(1,l2);
    56        int val =1;
    57        removeElements(l1,val);
    58    }
    59
    60}
     1package ch2;
    2
    3public class ReverseList {
    4
    5    private static class ListNode {
    6        int val;
    7        ListNode next;
    8
    9        ListNode() {
    10        }
    11
    12        ListNode(int val) {
    13            this.val = val;
    14        }
    15
    16        ListNode(int val, ListNode next) {
    17            this.val = val;
    18            this.next = next;
    19        }
    20
    21
    22    }
    23
    24    /**
    25     * 206. 反转链表
    26     * 反转一个单链表。
    27     * <p>
    28     * 示例:
    29     * <p>
    30     * 输入: 1->2->3->4->5->NULL
    31     * 输出: 5->4->3->2->1->NULL
    32     * 进阶:
    33     * 你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
    34     *
    35     * @param head
    36     * @return
    37     */

    38
    39    //迭代
    40    public static ListNode reverseList(ListNode head) {
    41        ListNode temp = null;
    42        ListNode pre = null, curr = head;
    43        while (curr != null) {
    44            temp = curr.next;
    45            curr.next = pre;
    46            pre = curr;
    47            curr = temp;
    48        }
    49        return pre;
    50    }
    51
    52    //递归
    53    public static ListNode reverseList1(ListNode head) {
    54        return reverse(null, head);
    55    }
    56
    57    private static ListNode reverse(ListNode pre, ListNode curr) {
    58        if (curr == null) {
    59            return pre;
    60        }
    61        ListNode temp = null;
    62        temp = curr.next;
    63        curr.next = pre;
    64        pre = curr;
    65        curr = temp;
    66        return reverse(pre, curr);
    67    }
    68
    69    public static void main(String[] args) {
    70        ListNode l4 = new ListNode(4);
    71        ListNode l3 = new ListNode(3, l4);
    72        ListNode l2 = new ListNode(2, l3);
    73        ListNode l1 = new ListNode(1, l2);
    74        reverseList(l1);
    75//        reverseList1(l1);
    76    }
    77}
    不会,我可以学;落后,我可以追赶;跌倒,我可以站起来!
  • 相关阅读:
    【Nginx 快速入门】反向代理、负载均衡、动静分离
    【Redis】缓存穿透、缓存击穿、缓存雪崩(面试必备)
    【Redis】特殊数据类型
    【Redis】特殊数据类型
    【Redis】特殊数据类型
    typescript 技巧学习
    angular9的学习(十九)
    angular11源码探索二十六[Router整体路由配置]
    angular11源码探索二十五[Router路由事件]
    angular11源码探索二十四[路由检测变更策略]
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/14367205.html
Copyright © 2011-2022 走看看