Leetcode鏈表

19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.

Example:

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.
題意:給出一個鏈表,數n,將這個鏈表的倒數第n個數刪除,然後在把這個鏈表輸出
思路:
使用兩個快慢指針,一個slow,一個fast
首先fast前進n個節點
然後slow從頭(head)和fast一起每次前進一個節點,當fast鏈表尾部的時候,
slow指向的就是倒數第n個節點
(看別人的博客)
JAVA

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
       ListNode fast=head;
       for(int i=0;i<n;i++)
       {
           fast=fast.next;
       }
        if(fast==null)
        {
            return head.next;
        }
        ListNode slow=head;
        while(fast.next!=null)
        {
            slow=slow.next;
            fast=fast.next;
        }
        slow.next=slow.next.next;
        return head;
        
    }
}

234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.

Example 1:

Input: 1->2
Output: false
Example 2:

Input: 1->2->2->1
Output: true
思路:快慢指針找到中間的位置,然後將後半部分的鏈表反轉,然後跟前半部分的鏈表逐個位置比對
JAVA

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow =slow.next;
        }
        if(fast != null) 
            slow =slow.next;
        slow = reverseList(slow);
        while(slow != null){
            if(slow.val != head.val)
                return false;
            slow = slow.next;
            head = head.next;
        }
        return true;
    }
    
    public ListNode reverseList(ListNode head){
        ListNode pre = null, next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}

JAVA2

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow =slow.next;
        }
        if(fast != null) 
            slow =slow.next;
        slow = reverseList(slow);
        while(slow != null){
        if(slow.val != head.val)
                return false;
            slow = slow.next;
            head = head.next;
        }
        return true;
    }
    
   public ListNode reverseList(ListNode head) {
        if(head==null)
        {
            return head;
        }
        ListNode p=head;
        ListNode q=head;
        while(head.next!=null)
        {
            p=head.next;
            head.next=head.next.next;
            p.next=q;
            q=p;
        }
        return q;
    }
}

21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
JAVA

/**
 * 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(l2.next,l1);
            return l2;
        }   
    }
}

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
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(l2->next,l1);
            return l2;
        }
        
    }
};

141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
在這裏插入圖片描述

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.

在這裏插入圖片描述
Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
在這裏插入圖片描述

題目要求是判斷鏈表是否有環
C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *p=head;
        ListNode *q=head;
        while(p!=NULL && p->next!=NULL )
        {
            q=q->next;
            p=p->next->next;
            if(q==p) return true;
        }
        return false;
        
    }
};

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 p=head;
        ListNode q=head;
        while(p!=null && p.next!=null )
        {
            q=q.next;
            p=p.next.next;
            if(q==p) return true;
        }
        return false;
        
    }
        
    }

206. Reverse Linked List
Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head==NULL)
        {
            return head;
        }
        ListNode *p=head;
        ListNode *q=head;
        while(head->next!=NULL)
        {
            p=head->next;
            head->next=head->next->next;
            p->next=q;
            q=p;
        }
        return q;
        
    }
};

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)
        {
            return head;
        }
        ListNode p=head;
        ListNode q=head;
        while(head.next!=null)
        {
            p=head.next;
            head.next=head.next.next;
            p.next=q;
            q=p;
        }
        return q;
        
    }
}

876. Middle of the Linked List
Given a non-empty, singly linked list with head node head, return a middle node of linked list.

If there are two middle nodes, return the second middle node.

Example 1:

Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3. (The judge’s serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.
Example 2:

Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.
JAVA

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
      if(head==null || head.next==null) 
        return head;
        ListNode slow=head;
        ListNode fast=head;
        fast=fast.next.next;
        while(fast!=null && fast.next!=null){
        slow=slow.next;
        fast=fast.next.next;
       }
       return slow.next;
   }
}

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        if(head==NULL || head->next==NULL) 
        return head;
        ListNode* slow=head;
        ListNode* fast=head;
        fast=fast->next->next;
        while(fast!=NULL && fast->next!=NULL){
        slow=slow->next;
        fast=fast->next->next;
       }
       return slow->next;
   }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章