LeetCode单链表相关题目

目录

1.移除链表元素(删除链表中等于给定值val的所有节点)

2.反转一个链表

3.找出链表的中间结点

4.输出链表中倒数第k个结点

5.合并两个有序链表,合并后依然有序

6.找出两个单链表相交的起始结点


1.移除链表元素(删除链表中等于给定值val的所有节点)

输入:1->2->6->3->4->5->6,val=6

输出:1->2->3->4->5

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode pre = head;
        
        if(head == null){
            return null;
        }else{
            ListNode cur = pre.next;
            while(cur != null){
                if(cur.val == val){
                    pre.next = cur.next;
                    cur = pre.next;
                }else{
                    pre = cur;
                    cur = cur.next;
                }
            }
            if(head.val == val){
                head = head.next;
            }
        }
        return head;
    }
}

2.反转一个链表

输入:1->2->3->4->5->null

输出:5->4->3->2->1->null

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

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

3.找出链表的中间结点

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

输入:[1,2,3,4,5]

输出:此列表中的结点3(序列化形式:[3,4,5])

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode cur = head;
        int length = 0;
        while(cur != null){
            length++;
            cur = cur.next;
        }
        int middleIndex = length/2;//2
        ListNode res = head;
        while(middleIndex != 0){
            res = res.next;
            middleIndex--;
        }
        return res;
    }
}

4.输出链表中倒数第k个结点

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head == null){
            return null;
        }
        int length = 0;
        ListNode cur = head;
        while(cur != null){
            length++;
            cur = cur.next;
        }
        if(k<=0 || k>length){
            return null;
        }
        int position = length-(k-1);
        int tmp = 1;
        cur = head;
        while(tmp != position){
            cur = cur.next;
            tmp++;
        }
        return cur;
    }
}

5.合并两个有序链表,合并后依然有序

输入:1->2->4, 1->3->4

输出:1->1->2->3->4->4

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null){
            return l2;
        }else if(l2 == null){
            return l1;
        }else if(l1.val < l2.val){
            l1.next = mergeTwoLists(l1.next,l2);
            return l1;
        }else{
            l2.next = mergeTwoLists(l1,l2.next);
            return l2;
        } 
    }
}

6.找出两个单链表相交的起始结点

分析一下,这两个单链表的状态应该是这样的:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pL = headA;
        ListNode pS = headB;
        //1.找出两个链表的长度差
        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 subLen = lenA - lenB;
        //A比B短
        if(subLen < 0){
            pL = headB;
            pS = headA;
            subLen = lenB - lenA;
        }
        //2.长的先走sublen步
        for(int i=0; i<subLen; i++){
            pL = pL.next;
        }
        //3.开始同时走
        while(pL != null && pS != null && pL != pS){
            pL = pL.next;
            pS = pS.next;
        }
        if(pL == pS && pL != null){
            return pL;
        }
        return null;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值