链表经典例题—数据结构

1. 删除链表中等于给定值 val 的所有节点

public void deleteNode(ListNode node) {
        ListNode nodeNext = node.next;
        node.next = nodeNext.next;
        node.val = nodeNext.val;        
    }

2. 反转一个单链表

public ListNode reverseList(ListNode head) {
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        //记录头结点的下一个节点
        ListNode cur = head.next;
        //将头结点置空
        head.next = null;
        while(cur != null){
            ListNode curNext = cur.next ;
            cur.next = head;//实现指向改变
            head = cur;
            cur = curNext;
        }
        return head;
    }

3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点

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

4. 输入一个链表,输出该链表中倒数第k个结点

public ListNode FindKthToTail(ListNode head,int k) {
        //记得考虑k不合法的情况
        if(k<=0||head==null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        //先让fast走k-1步
        while(k-1>0){
            fast = fast.next;
            //如果此时fast==null说明倒数k个节点不存在
            if(fast == null){
                return null;
            }
            k--;
        }
        while(fast.next!=null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

5. 将两个有序链表合并为一个新的有序链表并返回,新链表是通过拼接给定的两个链表的所有节点组成的

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null && list2 == null){
            return null;
        }
        ListNode newNode = new ListNode(-1);//虚拟节点数据,不具备意义
        ListNode tmp = newNode;
        while(list1 != null && list2 != null){
            if(list1.val < list2.val){
                tmp.next = list1;
                tmp = tmp.next;
                list1 = list1.next;
            }else{
                tmp.next = list2;
                tmp = tmp.next;
                list2 = list2.next;
            }
        }
        //到这一步说明此时list1和list2至少有一个是空的
        if(list1 == null){
            tmp.next = list2;
        }
        if(list2 == null){
            tmp.next = list1;
        }
        return newNode.next;
    }

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

public ListNode partition(ListNode pHead, int x) {
        if(pHead == null){
            return null;
        }
        ListNode cur = pHead;
        ListNode bs = null;//比x小的节点的头结点
        ListNode be = null;
        ListNode as = null;//比x大的节点的头结点
        ListNode ae = null;
        while(cur != null){
            if(cur.val < x){
                //注意插入第一个节点的时候头结点和尾结点相等
                if(bs == null){
                    bs = cur;
                    be = cur;
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else{
                if(as == null){
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }

            }
            cur = cur.next;
        }
        //考虑所有的节点都大于x
        if(bs == null){
            return as;
        }
        be.next = as;
        //考虑比x大的节点的尾结点不为空,记得置空
        if(ae!=null){
            ae.next = null;
        }
        return bs;
    }

7. 链表的回文结构

public boolean isPalindrome(ListNode head) {
        if(head == null || head.next == null){
            return true;
        }
        //找到链表的中间节点
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null&&fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        //翻转中间节点之后的节点
        ListNode cur = slow.next;
        while(cur!=null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //判断是不是回文
        while(head!=slow){
            if(slow.val != head.val){
                return false;
            }
            //考虑偶数节点的链表
            if(slow == head.next){
                return true;
            }
            head = head.next;
            slow =slow.next; 
        }
        return true; 
    }

8. 输入两个链表,找出它们的第一个公共结点## 标题

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        ListNode pl = headA;
        ListNode ps = headB;
        int lenA = 0;
        int lenB = 0;
        //求两个链表的长度
        while(pl!=null){
            lenA++;
            pl = pl.next;
        }
        while(ps!=null){
            lenB++;
            ps = ps.next;
        }
        pl = headA;
        ps = headB;
        int len = lenA - lenB;
        //保证pl指向长的链表,ps指向短的链表
        if(len<0){
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }
        //先让长的链表走len步
        while(len!=0){
            pl = pl.next;
            len--;
        }
        while(pl != ps){
            pl = pl.next;
            ps = ps.next;
        }
        if(ps == null&&pl == null){
            return null;
        }
        return pl;
    }

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

public boolean hasCycle(ListNode head) {
        if(head == null || head.next == 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;
    }

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

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;
        }
        fast = head;
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;        
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值