An article to deal with the interview in the list of topics (Java implementation)

Source: Internet
Author: User


       Data structure of a linked list
Class ListNode {
    listnode next;
    int Val;
    ListNode (int x) {
        val = x;
        next = null;
    }
}
1. Flip Linked list
ListNode Reverse (ListNode node) {
        ListNode prev = null;
        while (node!=null) {
            ListNode tmp = Node.next;
            Node.next = prev;
            prev = node;
            node = tmp;
        }
        return prev;
    }
    Flip list (Recursive way)
    ListNode Reverse2 (ListNode head) {
        if (Head.next = = null) {return head
            ;
        }
        ListNode Reversenode = Reverse2 (head.next);
        Head.next.next = head;
        Head.next = null;
        return reversenode;

    }
2. Determine if the linked list has a ring
    Boolean hascycle (ListNode head) {
        if (head = = null| | head.next = = NULL) {return
            false;
        }
        ListNode Slow,fast;
        Fast = Head.next;
        slow = head;
        while (Fast!=slow) {
            if (fast==null| | Fast.next==null) {return
                false;
            }
            Fast = Fast.next.next;
            slow = Slow.next;
        }
        return true;
    }
3, sorted list
    ListNode sortlist (ListNode head) {if (head = = null| | head.next = null) {return head;
        } ListNode mid = Middlenode (head);
        ListNode right = Sortlist (Mid.next);
        Mid.next = null;
        ListNode left = sortlist (head);
    Return to merge (left, right);
        } ListNode Middlenode (ListNode head) {ListNode slow = head;
        ListNode fast = Head.next;
            while (fast!=null&fast.next!=null) {slow = Slow.next;
        Fast = Fast.next.next;
    return slow;
        } listnode Merge (ListNode n1,listnode n2) {ListNode dummy = new ListNode (0);
        ListNode node = dummy;
                while (N1!=null&&n2!=null) {if (n1.val<n2.val) {node.next = n1;
            N1 = N1.next;
                }else{node.next = n2;
            N2 = N2.next;
        node = Node.next; } if (n1!=null) {node.neXT = N1;
        }else{node.next = n2;
    return dummy.next;
 }
4. List Add sum
    ListNode addlists (ListNode l1,listnode L2) {if (l1==null&&l2==null) {return null;
        } listnode head = new ListNode ();
        ListNode point = head;
        int carry = 0;
            while (l1!=null&&l2!=null) {int sum = carry + L1.val + l2.val;
            Point.next = new ListNode (SUM%10);
            carry = SUM/10;
            L1 = L1.next;
            L2 = L2.next;
        Point = Point.next;
            while (l1!=null) {int sum = carry + l1.val;
            Point.next = new ListNode (SUM%10);
            carry = SUM/10;
            L1 = L1.next;
        Point = Point.next;
            while (l2!=null) {int sum = carry + l2.val;
            Point.next = new ListNode (SUM%10);
            carry = SUM/10;
            L2 = L2.next;
        Point = Point.next;

        } if (carry!=0) {point.next = new ListNode (carry);
    return head.next;
 }
5. Get the reciprocal of the list n nodes
    ListNode nthtolast (ListNode head,int N) {
        if (head = = null| | n<1) {return
            null;
        }
        ListNode L1 = head;
        ListNode L2 = head;
        for (int i = 0;i<n-1;i++) {
            if (L2 = = null) {return
                null;
            }
            L2 = L2.next;
        }
        while (l2.next!=null) {
            L1 = l1.next;
            L2 = L2.next;
        }
        return L1;


    }
6. Delete the reciprocal nth node of the list
    ListNode Deletenthnode (ListNode head,int N) {
        //write your code here
        if (n <= 0) {return
            null;
        }

        ListNode dumy = new ListNode (0);
        Dumy.next = head;
        ListNode Prddel = dumy;
        for (int i = 0;i<n;i++) {
            if (head==null) {return
                null;
            }
            head = Head.next;
        }
        while (Head!=null) {head
            = Head.next;
            Prddel = Prddel.next;
        }
        Prddel.next = PrdDel.next.next;
        return dumy.next;

    }
7. Delete duplicate elements in a linked list
    ListNode Deletemunode (ListNode head) {
        if (head = = null) {return
            null;
        }
        ListNode node = head;
        while (Node.next!= null) {
            if (node.val = = node.next.val) {
                node.next = Node.next.next;
            } else{
                node = Node.next
            }
        }
        return head;

    }
8. Delete duplicate elements in linked list II, remove duplicate nodes
    ListNode DeleteMuNode2 (ListNode head) {
        if (head = = null| | Head.next = = null) {return head
            ;
        }
        ListNode dummy = new ListNode (0);
        Dummy.next = head;
        head = dummy;
        while (head.next!=null&&head.next.next!=null) {
            if (head.next.val = = head.next.next.val) {
                int val = Head.next.val;
                while (Head.next.val = = Val&&head.next!= null) {
                    head.next = head.next.next;
                }
            } else{head
                = Head.next
            }
        }
        return dummy.next;
    }
9. Rotating Chain List
    ListNode Rotateright (ListNode head,int k) {
        if (head ==null) {return
            null;
        }
        int length = GetLength (head);
        k = k% length;
        ListNode dummy = new ListNode (0);
        Dummy.next = head;
        head = dummy;
        ListNode tail = dummy;
        for (int i = 0;i<k;i++) {head
            = head.next;
        }
        while (head.next!= null) {head
            = Head.next;
            tail = Tail.next;
        }
        Head.next = Dummy.next;
        Dummy.next = Tail.next;
        Tail.next = null;
        return dummy.next;
    }
10. Rearrange linked list
    ListNode ReOrder (ListNode head) {
        if (head = = null| | Head.next = = null) {return
            ;
        }
        ListNode mid = Middlenode (head);
        ListNode tail = reverse (mid.next);
        Mergeindex (head, tail);
    private void Mergeindex (ListNode head1,listnode head2) {
        int index = 0;
        ListNode dummy = new ListNode (0);
        while (head1!=null&&head2!=null) {
            if (index%2==0) {
                dummy.next = head1;
                Head1 = Head1.next;
            } else{
                dummy.next = head2;
                head2 = Head2.next;
            }
            dummy = Dummy.next;
            Index + +;
        }
        if (head1!=null) {
            dummy.next = head1;
        } else{
            dummy.next = head2
        }
    }
11. Link List Division
    ListNode partition (ListNode head,int x) {
        if (head = = null) {return
            null;
        }
        ListNode left = new ListNode (0);
        ListNode right = new ListNode (0);
        ListNode leftdummy = left;
        ListNode rightdummy = right;
        while (head!=null) {
            if (head.val<x) {
                left.next = head;
                left = head;
            } else{
                right.next = head;
                right = head;
            }
            head = Head.next;
        }
        Left.next = Rightdummy.next;
        Right.next = null;
        return leftdummy.next;
    }
12. Flip the nodes between N and M of the linked list
    ListNode reversen2m (ListNode head,int m,int N) {
        if (m>=n| | Head = = null) {return head
            ;
        }
        ListNode dummy = new ListNode (0);
        Dummy.next = head;
        head = dummy;
        for (int i = 1;i<m;i++) {
            if (head = = null) {return
                null;
            }
            head = Head.next;
        }
        ListNode Pmnode = head;
        ListNode mnode = Head.next;
        ListNode nnode = Mnode;
        ListNode pnnode = Mnode.next;
        for (int i = m;i<n;i++) {
            if (Pnnode = = null) {return
                null;
            }
            ListNode tmp = Pnnode.next;
            Pnnode.next = Nnode;
            Nnode = Pnnode;
            Pnnode = tmp;
        }
        Pmnode.next = Nnode;
        Mnode.next = Pnnode;
        return dummy.next;
    }
13. Merge K sorted list
    ListNode Mergeklistnode (arraylist<listnode> k) {if (K.size () ==0) {return null;
    Return Mergehelper (K,0,k.size ()-1); } ListNode mergehelper (list<listnode> lists,int start,int end) {if (start = =) {return lis
        Ts.get (start);
        int mid = start + (End-start)/2;
        ListNode left = mergehelper (lists, start, mid);
        ListNode right = Mergehelper (lists, mid+1, end);
    Return mergetwolists (left,right);
        } ListNode mergetwolists (ListNode list1,listnode list2) {ListNode dummy = new ListNode (0);
        ListNode tail = dummy;
                while (List1!=null&&list2!=null) {if (list1.val<list2.val) {tail.next = List1;
                tail = Tail.next;
            List1 = List1.next;
                }else{tail.next = List2;
                tail = List2;
            List2 = List2.next; }} if(List1!=null)
        {tail.next = List1;
        }else{tail.next = List2;
    return dummy.next;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.