연쇄표 면접문제(一)

4824 단어
1. 단일 체인 시계를 반전시킨다.
public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }// 
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public Node reverseList(){
        Node reverseHead=null;// 
        Node prev=null;//cur 
        Node cur=this.head;//cur 
        while (cur!=null){
            Node curNext=cur.next;
            if(curNext==null){
                reverseHead=cur;
            }
            cur.next=prev;
            prev=cur;
            cur=curNext;
        }
        return reverseHead;
    }
    }

2. 체인 시계를 정하고 체인 시계가 링에 들어가기 시작한 첫 번째 노드를 되돌려준다.만약 체인 시계에 고리가 없다면,null로 돌아갑니다.
public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }// 
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public Node detectCycle(){
        Node fast=this.head;
        Node slow=this.head;
        while (fast!=null && fast.next.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow ){
                break;
            }
        }
        // 
        if(fast==null || fast.next.next==null){
            return null;
        }
        // slow , fast 
        slow=this.head;
        while (fast!=slow){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
    }

3. 체인 시계에 고리가 있는지 판단한다.
public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }// 
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public boolean hasCycle(){
        Node fast=this.head;
        Node slow=this.head;
        while (fast!=null && fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                return true;
            }
        }
        return false;
    }
    }

4. 연쇄표의 회문 구조.
public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }// 
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public boolean chkPalindrome(){
        Node fast=this.head;
        Node slow=this.head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        //slow 
        Node p=slow.next;
        Node pNext=p.next;
        while (p!=null){
            p.next=slow;
            slow=p;
            p=p.next;
            if(p!=null){
                pNext=p.next;
            }
        }
        // 
        while (this.head!=slow){
            if(this.head.data!=slow.data){
                return false;
            }
            if(this.head.next==slow){
                return true;
            }
            head=head.next;
            slow=slow.next;
        }
        return true;
    }
    }

5. 정렬된 체인 테이블에 중복된 결점이 존재하므로 이 체인 테이블에 중복된 결점을 삭제하십시오. 중복된 결점은 보류하지 않고 체인 헤드 바늘로 되돌아갑니다.
public class MySingleLinkedlmpl {
    class Node{
        public int getData() {
            return data;
        }
        private int data;
        public Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }// 
    private Node head;
    public MySingleLinkedlmpl(){
        this.head=null;
    }
    public Node deleteDuplication(){
        Node node=new Node(-1);
        Node cur=this.head;
        Node tmpHead=node;
        while (cur!=null){
            if(cur.next!=null&&cur.data==cur.next.data){
                while (cur.next!=null&&cur.data==cur.next.data){
                    cur=cur.next;
                }
                cur=cur.next;
                //cur 
                tmpHead.next=cur;
            }else{
                // , 
             tmpHead.next=cur;
             tmpHead=cur;
             cur=cur.next;
            }
        }
        return node.next;
    }
    }

좋은 웹페이지 즐겨찾기