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;
   }
};

JAVA3

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

猜你喜欢

转载自blog.csdn.net/qq_37617419/article/details/92847333