1. 程式人生 > >LeetCode之LinkedList前一百

LeetCode之LinkedList前一百

1.Reverse Linked List

反轉連結串列 Example:

Input: 1->2->3->4->5->NULL
Output: 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) { ListNode pre=null; ListNode curr=head; while(curr!=null){ ListNode nextTemp=curr.next; curr.next=pre; pre=curr; curr=nextTemp; } return pre; } }

2. Palindrome Linked List

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head)
{ ListNode slow=head; ListNode fast=head; while(fast!=null&&fast.next!=null){ fast=fast.next.next; slow=slow.next; } if(fast!=null){ slow=slow.next; } slow=reverse(slow); fast=head; while(slow!=null){ if(slow.val!=fast.val){ return false; } slow=slow.next; fast=fast.next; } return true; } public static ListNode reverse(ListNode head){ ListNode prev=null; ListNode curr=head; while(curr!=null){ ListNode nextTemp=curr.next; curr.next=prev; prev=curr; curr=nextTemp; } return prev; } }

3.Merge Two Sorted Lists

按序合併兩條連結串列 Example:

Input: 1->2->4, 1->3->4
Output: 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;
        if(l2==null) return l1;
        
        if(l1.val<=l2.val){
            l1.next=mergeTwoLists(l1.next,l2);
            return l1;
        }else{
            l2.next=mergeTwoLists(l1,l2.next);
            return l2;
        }
    }
}

4.Linked List Cycle

判斷連結串列是否有環

/**
 * 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) {
        if(head==null||head.next==null) return false;
        
        ListNode slow=head;
        ListNode fast=head.next;
        
        while(slow!=fast){
            if(fast==null||fast.next==null)
                return false;
            slow=slow.next;
            fast=fast.next.next;
        }
        return true;
    }
}

5.Intersection of Two Linked Lists

兩條連結串列相交,找出交點

For example, the following two linked lists:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3

begin to intersect at node c1.

確保兩個指標同時到達交叉點節點,在第一次迭代中,我們將在到達尾節點之後將一個連結串列的指標重置到另一個連結串列的頭部。在第二次迭代中,我們將移動兩個指標,直到它們指向同一個節點。我們在第一次迭代中的操作將幫助我們抵消差異。因此,如果兩個連結串列相交,則第二次迭代中的會合點必須是交點。

/**
 * 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 a=headA;
        ListNode b=headB;
 //if a & b have different len, then we will stop the loop after second iteration       
        while(a!=b){
        //for the end of first iteration, we just reset the pointer to the head of another linkedlist
            a=a==null?headB:a.next;
            b=b==null?headA:b.next;
        }
        return a;
    }
}