leetcode 328 --Odd Even Linked List 链表 双指针 移动节点

本文介绍了一种算法,用于将单链表中的奇数节点与偶数节点进行分离,实现奇数节点前置,偶数节点后置的操作。此操作在O(1)空间复杂度和O(nodes)时间复杂度下完成。

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.

Example:
Given 1->2->3->4->5->NULL,
return 1->3->5->2->4->NULL.

Note:
The relative order inside both the even and odd groups should remain as it was in the input.
The first node is considered odd, the second node even and so on …

解题思路:双指针移动,分别存放奇数节点和偶数节点,移动过程中把奇数节点和偶数节点换掉。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode oddEvenList(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode curr=head,nex=head.next,pre=nex;
        while(nex!=null&&nex.next!=null){
            curr.next=nex.next;
            curr=curr.next;
            nex.next=curr.next;
            nex=nex.next;
        }
        curr.next=pre;
        return head;
    }
}
LeetCode 234题回文链表有多种解法,以下为你介绍几种常见的解法: ### 解法一:将链表复制到数组里再从两头比对 将链表元素复制到数组中,然后使用双指针从数组的两端向中间遍历,比较对应元素是否相等。如果在遍历过程中发现不相等的元素,则链表不是回文链表;如果遍历完整个数组都没有发现不相等的元素,则链表是回文链表。 ```cpp #include <vector> class Solution { public: bool isPalindrome(ListNode* head) { std::vector<int> listvec; // 把链表中元素都插入数组 while (head != nullptr) { listvec.push_back(head->val); head = head->next; } // 一个迭代器从头,一个迭代器从尾 auto it_head = listvec.begin(); auto it_back = listvec.end() - 1; // 若迭代器相遇了则说明都检查完了,可以返回true while (it_back - it_head > 0) { // 检查到值不相等就返回false if ((*it_head++) != (*it_back--)) return false; } return true; } }; ``` ### 解法二:递归法 递归地遍历链表,同时使用一个指针链表头开始比较。递归过程中,先递归到链表的末尾,然后在回溯的过程中与链表指针指向的元素进行比较。 ```cpp class Solution { ListNode* frontPointer; public: bool recursivelyCheck(ListNode* currentNode) { if (currentNode != nullptr) { if (!recursivelyCheck(currentNode->next)) { return false; } if (currentNode->val != frontPointer->val) { return false; } frontPointer = frontPointer->next; } return true; } bool isPalindrome(ListNode* head) { frontPointer = head; return recursivelyCheck(head); } }; ``` ### 解法三:快慢指针 + 反转链表 使用快慢指针找到链表的中点,然后将链表的后半部分反转,最后比较前半部分和反转后的后半部分是否相等。 ```cpp class Solution { public: bool isPalindrome(ListNode* head) { if (head == nullptr) return true; // 找到前半部分链表的尾节点并反转后半部分链表 ListNode* firstHalfEnd = endOfFirstHalf(head); ListNode* secondHalfStart = reverseList(firstHalfEnd->next); // 判断是否回文 ListNode* p1 = head; ListNode* p2 = secondHalfStart; bool result = true; while (result && p2 != nullptr) { 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 = nullptr; ListNode* curr = head; while (curr != nullptr) { ListNode* nextTemp = curr->next; curr->next = prev; prev = curr; curr = nextTemp; } return prev; } ListNode* endOfFirstHalf(ListNode* head) { ListNode* fast = head; ListNode* slow = head; while (fast->next != nullptr && fast->next->next != nullptr) { fast = fast->next->next; slow = slow->next; } return slow; } }; ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值