234. 回文链表

这个博客主要介绍了如何使用Java实现链表的各种操作,包括判断链表是否为回文、在指定位置反转链表、分区链表以及按指定步长旋转链表。这些操作均在O(n)时间复杂度和O(1)空间复杂度下完成,展示了高效的数据结构处理能力。

请判断一个链表是否为回文链表。

示例 1:

输入: 1->2
输出: false

示例 2:

输入: 1->2->2->1
输出: true

进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

 

package Solution234;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Stack;

class Solution {
	public boolean isPalindrome(ListNode head) {
		Stack<ListNode> stk = new Stack<>();
		ListNode cur = head;
		while (cur != null) {// 放入stack
			stk.push(cur);
			cur = cur.next;
		}
		while (head != null && head.next != null) {

			if (head.val != stk.pop().val) {
				return false;
			}

			head = head.next;
		}
		return true;
	}

	public ListNode reverseBetween(ListNode head, int left, int right) {
		Stack<ListNode> stk = new Stack<ListNode>();

		if (head == null || head.next == null)
			return head;
		ListNode dummy1 = new ListNode(-1);
		ListNode s = dummy1;

		ListNode dummy2 = new ListNode(-1);
		ListNode b = dummy2;

		ListNode curr = head;
		int count = 1;
		while (curr != null) {
			if (count < left) {
				s.next = curr;
				s = s.next;
			}
			if (count > right) {
				b.next = curr;
				b = b.next;
			}
			if (count >= left && count <= right) {
				stk.push(curr);

			}

			curr = curr.next;
			count++;
		}
		b.next = null;
		while (!stk.isEmpty()) {
			s.next = stk.pop();
			s = s.next;
		}

		s.next = dummy2.next;
		return dummy1.next;

	}

	public ListNode partition(ListNode head, int x) {
		if (head == null || head.next == null)
			return head;
		ListNode dummy1 = new ListNode(-1);
		ListNode s = dummy1;

		ListNode dummy2 = new ListNode(-1);
		ListNode b = dummy2;

		ListNode curr = head;
		while (curr != null) {
			if (curr.val < x) {
				s.next = curr;
				s = s.next;
			} else {
				b.next = curr;
				b = b.next;
			}
			curr = curr.next;
		}
		b.next = null;
		s.next = dummy2.next;
		return dummy1.next;
	}

	public ListNode rotateRight(ListNode head, int k) {
		if (head == null) {
			return head;
		}
		if (k == 0) {
			return head;
		}

		int count = 0;
		ListNode start = head;
		// traverse till the end of the list and keep incrementing
		// count
		while (head.next != null) {
			head = head.next;
			count++;
		}
		count++;
		// if k > count, do k%count, its an optimization. 2%5 == 12%5
		k = k % count;
		// find the new k
		k = Math.abs(count - k);
		if (k == 0)
			return start;
		// connect last element to start
		head.next = start;
		// traverse for new k value
		while (k-- > 0) {
			head = head.next;
		}
		// note: head is not the last element, so set the start.
		start = head.next;
		head.next = null;
		return start;
	}

	public ListNode mergeKLists(ListNode[] lists) {
		if (lists == null || lists.length == 0)
			return null;

		PriorityQueue<ListNode> queue = new PriorityQueue<ListNode>(
				(Comparator<? super ListNode>) new Comparator<ListNode>() {
					public int compare(ListNode l1, ListNode l2) {
						return l1.val - l2.val;
					}
				});

		ListNode head = new ListNode(0);
		ListNode p = head;

		for (ListNode list : lists) {
			if (list != null)
				queue.offer(list);
		}

		while (!queue.isEmpty()) {
			ListNode n = queue.poll();
			p.next = n;
			p = p.next;

			if (n.next != null)
				queue.offer(n.next);
		}

		return head.next;

	}

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

	public ListNode removeElements(ListNode head, int val) {
		ListNode header = new ListNode(-1);
		header.next = head;
		ListNode cur = header;
		while (cur.next != null) {
			if (cur.next.val == val) {
				cur.next = cur.next.next;
			} else {
				cur = cur.next;
			}
		}
		return header.next;
	}

	void printList(ListNode node) {
		while (node != null) {
			System.out.print(node.val + " ");
			node = node.next;
		}
	}

	public static void main(String[] args) {

		Solution sol = new Solution();

		ListNode head = new ListNode(1);
		head.next = new ListNode(2);
		head.next.next = new ListNode(3);
		head.next.next.next = new ListNode(2);
		head.next.next.next.next = new ListNode(1);
		// head.next.next.next.next.next = new ListNode(2);

		System.out.println(sol.isPalindrome(head));
		sol.printList(head);

	}

}

 

### 判断回文链表的C++实现 为了判断一个单链表是否为回文链表,可以采用双端队列法或快慢指针加反转部分链表的方法。这里提供一种较为高效的算法——通过快慢指针找到链表中点并反转前半部分链表来进行比较。 #### 方法概述 利用两个速度不同的指针遍历整个列表:一个是每次移动一步(slow),另一个则是每两次才前进一次(fast)。当 fast 达到终点时,slow 正好位于中间位置。接着将 slow 后面的部分进行翻转并与前面未改变顺序的一半做对比即可得出结论[^1]。 下面是具体的 C++ 实现: ```cpp #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} }; bool isPalindrome(ListNode* head) { if (!head || !head->next) return true; // 空链表或只有一个元素的情况 ListNode *slow = head, *fast = head; stack<int> stk; while (fast != nullptr && fast->next != nullptr){ stk.push(slow->val); slow = slow->next; fast = fast->next->next; } // 如果长度为奇数,则跳过中心结点 if(fast!=nullptr){ slow=slow->next; } while (slow != nullptr){ int topVal = stk.top(); stk.pop(); if(topVal != slow -> val){ return false; }else{ slow = slow -> next; } } return true; } ``` 此段程序首先定义了一个 `ListNode` 类型用于表示链表中的每一个节点。函数 `isPalindrome()` 接受指向链表头部的指针作为参数,并返回布尔值指示该链表是否构成回文序列。其中运用到了栈的数据结构来保存前一半链表的信息以便后续匹配验证[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值