Leetcode reversal linked list and palindrome linked list

Reverse linked list (simple)

Let’s start with a simple reversal linked list, which is very useful. The other linked list questions will use the step of reversing the linked list.

Title description

Reverse a singly linked list.

Example:

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

Ideas

Recursion

  • The termination of recursion is: the current node is empty or its point is empty
  • Reverse the current node, first reverse the next node;
  • The next node points to the current node;
  • The current node points to empty;

Java code

/**
 * 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 newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}

Palindrome linked list

Look, it's useful

Title description

Please determine whether a linked list is a palindrome linked list.

Example 1:

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

Input: 1->2->2->1
Output: true
Advanced:
Can you solve this problem with O(n) time complexity and O(1) space complexity?

Ideas

It is only necessary to judge whether the node values of the first half and the second half of a linked list are the same one by one after the inversion .
How to find the first half and the second half?
The fast and slow pointer method is very useful in linked list questions, so remember. For example, it can find out which node from the bottom of the linked list. This is equivalent to finding the bottom half of the node.

  • First find the first and second half
  • Reverse the second half (the recursive method of the previous question)
  • Compare the node values ​​of the front and back half one by one

Java code

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    //递归反转一个链表
    public ListNode reverse(ListNode head){
        if(head.next == null) return head;
        ListNode newHead = reverse(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
    
    public boolean isPalindrome(ListNode head) {
        //特殊情况
        if(head ==null || head.next == null) return true;
        //快慢指针,寻找中点
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next!=null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        //翻转后半段
        slow = reverse(slow.next);
        //对比
        while(slow!=null){
            if(head.val!=slow.val) return false;
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
}

Guess you like

Origin blog.csdn.net/fxjzzyo/article/details/87968416