Leetcode刷题--链表篇(找环,归并排序,合并链表,链表反转,相交链表)

1. Leetcode-206:反转链表

这道题的经典做法:断链+头插法,以下为java实现的代码:

/**
 * 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 || head.next==null)
            return head;
        ListNode first = head;
        ListNode second = first.next;
        first.next=null;  //断链  将链表中的首元节点为一个链,剩下的节点为另外一个链
        while(second!=null){   //使用头插法将剩下的节点一个一个插入到首元节点之前
            ListNode temp = second.next;
            second.next=first;
            first=second;
            second=temp;
        }
        return first;
    }
}

2、Leetcode-21:合并两个有序链表

以下为Java实现的代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null && l2==null){
            return l1;
        }
        if(l1==null && l2!=null){
            return l2;
        }
        if(l1!=null && l2==null){
            return l1;
        }
        ListNode head = new ListNode(0);
        ListNode current = head;
        while(l1!=null && l2!=null){
            if(l1.val<l2.val){
                current.next=l1;
                current=current.next;
                l1=l1.next;
            } else{
                current.next=l2;
                current=current.next;
                l2=l2.next;
            }
        }
        if(l1==null){
            current.next=l2;
        } else{
            current.next=l1;
        }
        return head.next;
    }
}

3、 Leetcode-141:环形链表

 经典做法:使用快慢指针

以下为Java实现代码:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
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;
    }
}

4、Leetcode-148:排序链表

经典做法:使用归并进行排序

以下为Java实现代码:
 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        if(head==null || head.next==null){
            return head;
        }
        ListNode mid = getMid(head);
        ListNode nhalf = mid.next;
        mid.next=null;
        return merge(sortList(head),sortList(nhalf));
    }
    public ListNode getMid(ListNode head){
        if(head==null){
            return head;
        }
        //使用快慢指针 找出链表的中间节点
        ListNode slow,fast;
        slow=fast=head;
        while(fast.next!=null && fast.next.next!=null){
            slow=slow.next;
            fast = fast.next.next;
        } 
        return slow;
    }
    //合并排好序的链表
    public ListNode merge(ListNode a,ListNode b){
        ListNode dhead,current;
        dhead = new ListNode(0);
        current = dhead;
        while(a!=null && b!=null){
            if(a.val<b.val){
                current.next=a;
                a=a.next;
            }else{
                current.next=b;
                b=b.next;
            }
            current=current.next;
        }
        current.next = (a==null)?b:a;
        return dhead.next;
    }
}

5、Leetcode-160:相交链表

以下为Java实现代码:

/**
 * 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) {
        if(headA==null || headB==null)
            return null;
        ListNode pA = headA;
        ListNode pB = headB;
        while(pA!=pB){
            if(pA==null){
                pA=headB;
            } else{
                pA=pA.next;
            }
            if(pB==null){
                pB=headA;
            } else{
                pB=pB.next;
            }
        }
        return pA;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值