Linked list questions basic skills ListNode

Linked list problem skills

Here we talk about two techniques for linked list questions, the commonly used reverse linked list and linked list halving. It can be used in many questions. Here is a question about "Palindrome Linked List", which uses these two techniques.

reverse linked list

Reversing the linked list can use O(n) time and O(1) space to get the data in reverse order

	private ListNode reverseList(ListNode head) {
    
    
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
    
    
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }

Linked list folded in half

Obtaining the midpoint of the linked list can be used to judge the palindrome, and the concept of fast and slow pointers is also used

	private ListNode endOfFirstHalf(ListNode head) {
    
    
        if (head == null) return null; 
        ListNode fast = head;
        ListNode slow = head;
        while (fast.next != null && fast.next.next != null) {
    
    
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

Topic: Palindrome Linked List

Write a function that checks whether the input linked list is palindromic.

Input: 1->2
Output: false

Input: 1->2->2->1
Output: true

untie:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public boolean isPalindrome(ListNode head) {
    
    
        if (head == null) {
    
    
            return true;
        }

        // 找到前半部分链表的尾节点并反转后半部分链表
        ListNode firstHalfEnd = endOfFirstHalf(head);
        ListNode secondHalfStart = reverseList(firstHalfEnd.next);

        // 判断是否回文
        ListNode p1 = head;
        ListNode p2 = secondHalfStart;
        boolean result = true;
        while (result && p2 != null) {
    
    
            if (p1.val != p2.val) {
    
    
                result = false;
            }
            p1 = p1.next;
            p2 = p2.next;
        }        

        // 还原链表并返回结果, 可省略
        firstHalfEnd.next = reverseList(secondHalfStart);
        return result;
    }

    private ListNode reverseList(ListNode head) {
    
    
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
    
    
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }

    private ListNode endOfFirstHalf(ListNode head) {
    
    
        if (head == null) return null; 
        ListNode fast = head;
        ListNode slow = head;
        while (fast.next != null && fast.next.next != null) {
    
    
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

Guess you like

Origin blog.csdn.net/w907645377/article/details/130376390