• 单向链表和双向链表的反转


    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;
        }
    }
  • 相关阅读:
    Lightoj 1023
    Tju 4119. HDFS
    Lightoj 1020
    Lightoj 1019
    小奇挖矿 2(4和7)
    [AHOI2012]树屋阶梯
    漂亮字串
    Prison 监狱
    2-XOR-SAT
    牛宫
  • 原文地址:https://www.cnblogs.com/yanghailu/p/12773249.html
Copyright © 2020-2023  润新知