zoukankan      html  css  js  c++  java
  • 单向链表和双向链表的反转

    package arithmetic;
    
    public class ReverseList {
    
        public static class Node {
            public int value;
            public Node next;
    
            public Node(int data) {
                value = data;
            }
        }
    
        public static class DoubleNode {
            public int value;
            public DoubleNode last;
            public DoubleNode next;
    
            public DoubleNode(int data) {
                value = data;
            }
        }
    
        public static Node reverseLinkedList(Node head) {
            Node pre = null;
            Node next = null;
            while (head != null) {
                next = head.next;
    
                head.next = pre;
                pre = head;
    
                head = next;
            }
            return pre;
        }
    
        public static DoubleNode reverseDoubleList(DoubleNode head) {
            DoubleNode pre=null;
            DoubleNode next=null;
            while (head!=null){
                next= head.next;
    
                head.next=pre;
                head.last=next;
                pre=head;
    
                head=next;
            }
            return pre;
        }
    
        public static Node removeValue(Node head, int num) {
            while (head!=null){
                if (head.value!=num){
                    break;
                }
                head= head.next;
            }
    
            //pre为上一个不为num的节点
            Node pre=head;
            //head不动了,最后返回用head,操作当前的cur
            //找到第一个不需要删的位置
            Node cur=head;
    
            while (cur!=null){
                if (cur.value==num){
                    //移除cur元素,将上一节点pre的next指针指向cur的next指针
                    pre.next=cur.next;
                }else{
                    pre=cur;
                }
                //移动一位
                cur=cur.next;
            }
            return head;
        }
    }
  • 相关阅读:
    js 和 jquery的宽高
    client、offset、scroll
    web开发中会话跟踪的方法有哪些
    前端需要注意哪些SEO
    ES6 Set和Map数据结构
    ES6实现数组去重
    ES6 Symbol
    ES6对象的拓展
    ES6数组的拓展
    ES6函数的拓展
  • 原文地址:https://www.cnblogs.com/yanghailu/p/12773249.html
Copyright © 2011-2022 走看看