1.判断链表中是否有环
public class Solution { public boolean hasCycle(ListNode head) { if (head == null) return false; ListNode slow = head; ListNode fast = head; while(fast!=null && fast.next!=null){ slow = slow.next; fast = fast.next.next; if(slow == fast) return true; } return false; } }
2.归并有序链表
public ListNode mergeTwoLists (ListNode l1, ListNode l2) { ListNode head = new ListNode(-1); ListNode curr = head; while(l1!=null && l2!=null){ if(l1.val>l2.val){ curr.next = l2; l2 = l2.next; }else{ curr.next = l1; l1 = l1.next; } curr = curr.next; } curr.next=l1==null?l2:l1; return head.next; } }
原文:https://www.cnblogs.com/augenstern/p/13662674.html