Determine the file back to a single list


START input ⼀ single head of the list nodes, it is determined that the number is not in the list back to the file

/**
* 单链表节点的定义:
* public class ListNode {
* int val;
* ListNode next;
* }
*/
boolean isPalindrome(ListNode head);

Input: 1-> 2-> null output: false
Input: 1-> 2-> 2-> 1- > null output: true

Analyzing palindrome, the most commonly used is the double pointer.
But not a single linked list traversal backwards, can not use double pointer

Method One: reverse list

The reversed original list, a new list is stored, and then compare these two lists whether the same
time and space complexity of the algorithm is O (N)

Method 2: traversing Binary Tree - Stack Structure

After ⼆ tree by traversing idea, no explicit inverted original list may traverse the list in reverse order

List both recursive structure, tree structure but a list of derivatives ⽣. Well, the list can actually have a preorder traversal and postorder:

void traverse(ListNode head) {
	// 前序遍历代码
	traverse(head.next);
	// 后序遍历代码
}

If I want to order printing val positive values in the linked list, you can write code pre-order traversing position;
the other hand, if you want to reverse traversing the list, can operate in a postorder traversal position:

//倒序打印单链表中的元素值
void traverse(ListNode head){
	if(head == null) return;
	traverse(head.next);
	//后序遍历代码
	print(head.val);
}

Mimic double pointer back to the file is determined to achieve a function
of time and space complexity of the algorithm is O (N)

//左侧指针
ListNode left;

boolean isPalidrome(ListNode head){
	left = head;
	return traverse(head);
}
boolean traverse(ListNode right){
	if(right == null) return true; //递归终点:right到达最右侧 ==null
	boolean res = traverse(right.next);
	//后序遍历代码
	res = res && (right.val == left.val); //right到达最右侧返回后,递归里的right是最后一个节点,res是true
	left = left.next;
	return res;
}

Nuclear Center Weighted logic: in fact, the list node START ⼀ put a stack, then a chance, this time the order of the elements is the opposite, but our interest is to stack Using recursive functions ⽽ already.

Method three: only partially reversed after the mid-point list

The overall time complexity of the algorithm is O (N), the spatial complexity is O (1)

  1. To find the midpoint of the first list by [bis pointer] Pointer speed
ListNode slow, fast;
slow = fast = head;
while(fast != null && fast.next){ //根据fast判断while条件,fast走到最后的前一个位置,下一步需要分情况
	slow = slow..next;
	fast = fast.next.next;
}	
//slow指针现在指向链表中点

Here Insert Picture Description
2. If the pointer does not point fast null, list description length is an odd number, so that slow step further and
this inversion is slow starting
Here Insert Picture Description
3. slow start from behind the inverted list, and compare the palindromic sequence
Here Insert Picture Description

ListNode left = head;
ListNode right = reverse(slow); //reverse函数返回反转后的头结点,就是right位置

while(right != null){ // 比较回文串,right指针的终点是null
	if(left.val != right.val)
		return false;   
	left = left.next;
	right = right.next;
}
return true;

Non-recursive inversion, a reversal to a node

ListNode reverse(ListNode head){
	ListNode pre = null, cur = head;
	while(cur != null){
		ListNode next = cur.next;
		cur.next = pre;
		pre = cur;
		cur = next;
	}
	return pre;
}
  1. Can retain the original chain structure of the input
    key is obtained p, q both hand position
    just before the function return plus one Hash code to restore the original order of the list: p.next = reverse (q);
    ** here ** Insert Picture Description
Published 149 original articles · won praise 5 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_26327971/article/details/105209312