Linked list exercise (make progress a little every day)

Posted by habs20 on Sun, 19 Dec 2021 06:11:16 +0100

Linked list exercises

1, Delete all nodes in the linked list equal to the given value val

OJ link

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head==null)return null;
        ListNode ret=head;
        ListNode cur=head.next;
        while(cur!=null){
            if(cur.val==val){
                ret.next=cur.next;
                cur=cur.next;
            }else{
                ret=ret.next;
                cur=cur.next;
            }
        }
        if(head.val==val){
                head=head.next;
            }
        return head;
    }
}

2, Flip single linked list

Interview test
Force buckle link

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null)return null;
        if(head.next==null) return head;
        ListNode cur=head;
        ListNode trev=head.next;
        cur.next=null;
        cur=trev;
        while(cur!=null){
            trev=trev.next;
            cur.next=head;
            head=cur;
            cur=trev;
        }
        return head;
    }
}

3, Linked list intermediate node

Given a non empty single linked list with head node, return the intermediate node of the linked list. If there are two intermediate nodes, the second intermediate node is returned.
OJ link

class Solution {
    public ListNode middleNode(ListNode head) {
        if(head==null)return null;
        ListNode slow = head;
        ListNode fast = head;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        return slow;
    }
}

4, The penultimate node in the linked list

Input a linked list and output the penultimate node in the linked list.
Niuke link

public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head==null)return null;
        ListNode fast = head;
        ListNode slow = head;
        while(k-1!=0){
            if(fast.next!=null){
                fast=fast.next;
                k--;
            }else{
                return null;
            }
        }
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
}

5, Linked list segmentation

There is a header pointer ListNode* pHead of a linked list. Given a certain value of X, write a piece of code to rank all nodes less than x before the other nodes, and the original data order cannot be changed. Return the header pointer of the rearranged linked list.
Niuke link

public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        if(pHead==null)return null;
        ListNode cur=pHead;
        ListNode fast1=null;
        ListNode fast2=null;
        ListNode slow1=null;
        ListNode slow2=null;
        while(cur!=null){
            if(cur.val<x){
                if(fast1==null){
                    fast1=cur;
                    fast2=cur;
                }else{
                    fast2.next=cur;
                    fast2=fast2.next;
                }
            }else{
                if(slow1==null){
                    slow1=cur;
                    slow2=cur;
                }else{
                    slow2.next=cur;
                    slow2=slow2.next;
                }
            }
            cur=cur.next;
        }
        if(fast2==null){
            return slow1;
        }
        fast2.next=slow1;
        if(slow1!=null){
            slow2.next=null;
            return fast1;
        }
        return fast1;
    }
}

6, Delete duplicate nodes in the linked list

In a sorted linked list, there are duplicate nodes. Please delete the duplicate nodes in the linked list. The duplicate nodes are not retained and the chain header pointer is returned. For example, the linked list 1 - > 2 - > 3 - > 3 - > 4 - > 4 - > 5 is 1 - > 2 - > 5 after processing
Niuke link

public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        if(pHead==null)return null;
        ListNode cur=pHead;
        ListNode tmpHead=new ListNode(-1);
                ListNode newHead=tmpHead;
        while(cur!=null){
            if(cur.next != null && cur.val == cur.next.val){
                while(cur.next != null && cur.val == cur.next.val){
                    cur=cur.next;
                }
            }else{
                tmpHead.next=cur;
                tmpHead=tmpHead.next;
            }
            cur=cur.next;
        }
        tmpHead.next=null;
        return newHead.next;
    }
}

7, Palindrome structure of linked list

Niuke link
For a linked list, please design an algorithm with time complexity of O(n) and additional space complexity of O(1) to judge whether it is palindrome structure.

Given the header pointer A of A linked list, please return A bool value to represent whether it is A palindrome structure. Ensure that the length of the linked list is less than or equal to 900.

public class PalindromeList {
    public boolean chkPalindrome(ListNode A) {
        // write code here
        if(A == null)return false;
        if(A.next == null)return true;
        ListNode fast = A;
        ListNode slow = A;
        //Find intermediate node
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        //Flip linked list
        ListNode cur = slow.next;
        while(cur!=null){
            ListNode tmp =cur.next;
            cur.next=slow;
            slow=cur;
            cur=tmp;
        }
        //judge
        while(slow != A){
            //Odd case
            if(slow.val!= A.val){
                return false;
            }
            //Even case
            if(slow == A.next){
                return true;
            }
            slow=slow.next;
            A=A.next;
        }
        return true;
    }
}

8, Circular linked list

OJ link
Given a linked list, judge whether there are links in the linked list.

If there is a node in the linked list that can be reached again by continuously tracking the next pointer, there is a ring in the linked list. In order to represent the rings in a given linked list, we use the integer pos to represent the position where the tail of the linked list is connected to the linked list (the index starts from 0). If pos is - 1, there are no rings in the linked list. Note: pos is not passed as a parameter, but only to identify the actual situation of the linked list.

Returns true if there are links in the linked list. Otherwise, false is returned.

public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }
}

It is essentially an application of fast and slow pointers.

9, Return to loop node

OJ link
Given a linked list, return the first node from the linked list into the ring. If the linked list is acyclic, null is returned.

In order to represent the rings in a given list, we use the integer pos to represent the position where the tail of the list is connected to the list (the index starts from 0). If pos is - 1, there is no ring in the list. Note that pos is only used to identify the ring and will not be passed to the function as a parameter.

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode 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;
        }
        slow=head;
        while(fast != slow){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
}

10, Merge two ordered linked lists

OJ link
Merge the two ascending linked lists into a new ascending linked list and return. The new linked list is composed of all nodes of a given two linked lists.

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode tmpHead = new ListNode();
        ListNode newHead = tmpHead;
        while(l1 != null && l2 != null){
            if(l1.val > l2.val){
                tmpHead.next = l2;
                l2=l2.next;
                tmpHead=tmpHead.next;
            }else{
                tmpHead.next = l1;
                l1=l1.next;
                tmpHead=tmpHead.next;
            }
        }
        if(l1==null){
            tmpHead.next=l2;
        }
        if(l2==null){
            tmpHead.next=l1;
        }
        return newHead.next;
    }
}

11, Intersecting linked list

OJ link
Here are the head nodes headA and headB of the two single linked lists. Please find and return the starting node where the two single linked lists intersect. If two linked lists have no intersection, null is returned.

public class Solution {
    public int getLength(ListNode head){   //Find length
        int i=0;
        while(head != null){
            head=head.next;
            i++;
        }
        return i;
    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null && headB==null)return null; 
        ListNode pl=headA;
        ListNode ps=headB;
        int a = getLength(headA);
        int b = getLength(headB);
        int c = a-b;
        if(c < 0){  
            pl=headB;
            ps=headA; 
            c = b-a;
        }
        while(c != 0){
            pl=pl.next;
            c--;
        }
        while(pl != ps){
            pl=pl.next;
            ps=ps.next;
        }
        if(pl == null) return null;
        return pl;
    }
}

Topics: data structure linked list