实现单链表的进本操作(2)

这篇博客详细介绍了如何实现单链表的基本操作,包括以给定值分割链表、删除排序链表中的重复结点、判断链表是否为回文结构、查找两个链表的首个公共结点以及检测链表中的环。通过这些操作,深入理解链表的数据结构和算法应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前

public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        if(pHead == null) {
            return null;
        }
        if(pHead.next == null) {
            return pHead;
        }
        ListNode smallHead = new ListNode(1);
        ListNode smallTail = smallHead;
        ListNode bigHead = new ListNode(1);
        ListNode bigTail = bigHead;
        ListNode cur = pHead;
        while(cur != null) {
            if(cur.val < x) {
                smallTail.next = new ListNode(cur.val);
                smallTail = smallTail.next;
                cur = cur.next;
            }else {
                bigTail.next = new ListNode(cur.val);
                bigTail = bigTail.next;
                cur = cur.next;
            }
        }
        smallTail.next = bigHead.next;
        return smallHead.next;
    }
}

2.在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针

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

3.判定链表的回文结构

public class PalindromeList {
    public boolean chkPalindrome(ListNode A) {
        ListNode B = reverseList(A);
        while(A != null) {
            if(A.val != B.val) {
                return false;
            }
            A = A.next;
            B = B.next;
        }
        return true;
    }
    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode newHead = null;
        ListNode cur = head;
        ListNode prev = null;
        while(cur != null) {
            ListNode next = cur.next;
            if(next == null) {
                newHead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return newHead;
    }
}

4.输入两个链表,找出它们的第一个公共结点

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lenA = size(headA);
        int lenB = size(headB);
        if(lenA > lenB) {
            int offset = lenA-lenB;
            for(int i = 0; i < offset;i++) {
                headA = headA.next;
            }
        }else {
            int offset = lenB - lenA;
            for(int i = 0;i < offset;i++) {
                headB = headB.next;
            } 
        }
        while(headA != null && headB != null) {
            if(headA == headB) {
                return headA;
            }
            headA = headA.next;
            headB = headB.next;
        }
        return null;
    }
    public int size(ListNode head) {
        int size = 0;
        for(ListNode cur = head;cur != null; cur = cur.next) {
            size++;
        }
        return size;
    }
}

5.给定一个链表,判断链表中是否有环

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null) {
            return false;
        }
        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;
    }
}

6.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null) {
            return null;
        }
        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;
        }
        ListNode cur1 = head;
        ListNode cur2 = fast;
        while(cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值