Summary problem with java achieve a single linked list

Each node contains a single list of all values, further comprising a next node linked reference field. List all the nodes sequentially linked to organized. The above is the basic definition of the list, recently wrote a number of single-linked list, also made some think, of course I thought might be missing or wrong, write code robustness may not be good, if there is an error or better the method welcome to correct me. First we summarize the single list, then find time to sum up doubly linked list.

First, the list is defined as follows:

1 public class Node {
2 
3     Node next = null;
4     int val;
5     public Node(int x) {
6         val = x;
7     }
8 }

 

Create a class that implements some questions about singly linked list, first define the variable head

Node head = null;

 

Increasing the linked list implementation: on the list increases, in fact, can be summarized as a method, but here divided into a scratch, tail, and the middle insertion node

// list the new node is inserted into the head 
    public  void the addFirst ( int value) {
        Node node = new Node(value);
        if(head != null) {
            node.next = head;
            head = node;
        }
        head = node;
    }
    // list the new node is inserted into the tail 
    public  void addLast ( int value) {
         / * the Node Node new new = the Node (value);
        Node cur = head;
        if (head == null) {
            cur = node;
            return;
        }
        while(cur != null) {
            cur = cur.next;
        }
        cur.next = node;*/
        add(length(), value);
    }
    // list length 
    public  int length () {
         int length = 0 ;
        Node cur = head;
        while(cur != null) {
            length++;
            cur = cur.next;
        }
        return length;
    }

    // intermediate insert list 
    public  void the Add ( int index, int value) {
        CUR the Node = head;
         IF (index <0 || index> length ()) {
             the throw  new new an IllegalArgumentException ( "illegal operation, the index does not exist" );
        }
        if (index == 0) {
            addFirst(value);
        }else {
            for (int i = 0; i < index-1 ;i++) {
                cur = cur.next;
            }
            Node node = new Node(value);
            node.next = cur.next;
            cur.next = node;
        }
    }

Chain to realize the printing function is in fact linked list traversal:

// list print 
    public  void printList () {
        Node cur = head;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }

 

I delete the list of writing a method to delete a node to operate according to the index

// list delete 
    public  void the Delete ( int index) {
        Node pre = head;
        
        IF (index <0 || index> length ()) {
             the throw  new new an IllegalArgumentException ( "illegal operation, the index does not exist" );
        }
        if (index == 0) {
            if(head != null) {
                head = head.next;
                return;
            }else {
                System.out.println ( "first node is empty" );
            }
        }
        else {
            for(int i = 0; i < index -1; i++) {
                pre = pre.next;
            }
            pre.next = pre.next.next;
        }
    }

 

Query a list of nodes according to the index, and returns the value of a node

// list query returns the index value 
    public  int findNode ( int index) {
        Node pre = head;
        
        IF (index <0 || index> length ()) {
             the throw  new new an IllegalArgumentException ( "illegal operation, the index does not exist" );
        }else {
            if (head == null) {
                return 0;
            }
            else {
                for (int i = 0 ;i < index ; i++) {
                    pre = pre.next;
                }
            }
        }
        return pre.val;
    }

 

Find penultimate n nodes, the idea here is to define a single list two nodes pointing to the head, such as a and B, so that a node go ahead n steps, then a, b while traversing node, when a walk is null, b n just come penultimate node. The following destination node index is the inverse of the index value, the relationship between the two is index = n-1.

public int findRevNode(int index) {
        Node before = head;
        Behind the Node = head;
         IF (index <0 || index> length ()) {
             the throw  new new an IllegalArgumentException ( "illegal operation, the index does not exist" );
        } else {
            if (head == null) {
                return 0;
            }else {
                for(int i = 0;i < index;i++) {
                    before = before.next;
                }
                while(before.next != null) {
                    before = before.next;
                    behind = behind.next;
                }
            }
        }
        return behind.val;
    }

 

When the idea of ​​the inverted list, define three nodes, respectively, in the front, after the node in the node point, before the final node to the last node traversed, imparting to the head node

// inverted list 
    public  void ReverseLink () {
        CurNode the Node = head; // first node 
        the Node preNode = null ; // a node before 
        the while (curNode =! Null ) {
            NextNode the Node = curNode.next; // Reserved next node 
            curNode.next = preNode; // pointer inverted 
            preNode = curNode; // front junction shift 
            curNode = nextNode; // the current node shift 
        }
        head = preNode;
    }

 

A ring chain, fast and slow analogy of people jogging track in the ring, people will fast and slow people meet. And the loop point problem.

// list a cycloalkyl 
    public  Boolean hasCycle (the Node head) {
         IF (head.next == null ) {
             return  to false ;
        }
        Node fast = head;
        Node slow = head;
        
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }
    
    // list the ring node 
    public the Node findPort (the Node head) {
         // first determine whether there is a ring 
        the Node = FAST head;
        Node slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        
        // equal to the head of the list to the loop entry point (n - 1) + meeting point to the inner circulation loop entry point,
         // provided a list head pointer entry points and the ring, respectively,
         // starting at the same time, each step in the walk , they must be met, and the first ring meet point is an entry point. 
        = SLOW head;
         the while (! = SLOW FAST) {
            slow = slow.next;
            fast = fast.next;
            
        }
        return slow;
    }

 

Two lists intersection and intersection points may be likened to the shape of y horizontally, there must be an intersection point, and the same node behind

// two lists intersect 
    public  Boolean Point (the Node head1, the Node head2 The) {
         IF (head1 == null || head2 The == null ) {
             return  to false ;
        }
        while(head1.next != null) {
            head1 = head1.next;
        }
        while (head2.next != null) {
            head2 = head2.next;
        }
        if(head1 == head2) {
            return true;
        }
        else {
            return false;
        }
    }
    
    // intersection point of the list of 
    public the Node findPoint (the Node head1, the Node head2 The) {
         IF (head1 == null || head2 The == null ) {
             return  null ;
        }
        Node p1 = head1;
        Node p2 = head2;
        int len1 = 0;
        int len2 = 0;
        int diff = 0;
        
        while(p1.next != null) {
            p1 = p1.next;    
            len1 ++ ;
        }
        while(p2.next != null) {
            p2 = p2.next;
            len2++;
        }
        if (p1 != p2) {
            return null;
        }
        diff = Math.abs(len1-len2);
        if(len1 > len2) {
            p1 = head1;
            p2 = head2;
        }else {
            p1 = head2;
            p2 = head1;
        }
        for (int i = 0; i < diff; i++) {
            p1 = p1.next;
        }
        while(p1 != p2) {
            p1 = p1.next;
            p2 = p2.next;
        }
        return p1;
    }

 

The above is a single list of exercises and summary.

 

 

Guess you like

Origin www.cnblogs.com/mosachan/p/11909394.html