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;
        }
    }
  • 相关阅读:
    算法初步——贪心
    sql去除重复记录 且保留id最小的 没用
    项目二:品优购 第三天
    Restful
    lucene 第一天
    lucene和solr
    zookeeper 面试题 有用
    dubbo 相关面试题 有用
    webservice CXF 相关面试题
    POI技术
  • 原文地址:https://www.cnblogs.com/yanghailu/p/12773249.html
Copyright © 2011-2022 走看看