LINTCODE——K组翻转链表

LINTCODE——K组翻转链表

思路:和翻转链表差不多,就是要注意每隔K个值打断、翻转、拼接就好了

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */


class Solution {
public:
    /*
     * @param head: a ListNode
     * @param k: An integer
     * @return: a ListNode
     */
    ListNode * reverseKGroup(ListNode * head, int k) {
        // write your code here


        ListNode *theadNode = new ListNode(0);
            theadNode -> next = head;
        ListNode *resNode = theadNode;
        ListNode *curNode = head;
        int cnt = 0;
        while(curNode != NULL)
        {
            cnt++;
            if(cnt == k)
            {
                //打断
                ListNode *tcurNode = curNode;
                curNode =  curNode -> next;
                tcurNode -> next = NULL;
                //翻转
                head = reverse(head);

                //拼接
                theadNode -> next = head;
                ListNode *tail = head;
                while(tail->next)
                    tail = tail->next;
                tail -> next = curNode;

                //重新定义头节点
                theadNode = tail;
                head = curNode;

                cnt = 0;
                continue;

            }

            curNode =  curNode -> next;

        }
        return resNode -> next;


    }
    ListNode *reverse(ListNode *head)
    {
        if(head == NULL)
            return head;
        ListNode *curNode = head;
        ListNode *preNode = NULL;
        while(curNode != NULL)
        {
            ListNode *nextNode = curNode -> next;
            curNode -> next = preNode;
            preNode = curNode;
            curNode = nextNode;
        }
        return preNode;

    }
};
### 如何使用 JavaScript 实现 K 个一翻转链表 为了实现这个功能,可以创建一个函数 `reverseKGroup` 来处理这个问题。此方法涉及遍历链表并按每 k 个节点反转一次子列表。 #### 定义链表结构 首先定义单向链表的数据结构: ```javascript class ListNode { constructor(val, next = null) { this.val = val; this.next = next; } } ``` #### 反转链表的核心逻辑 接下来编写核心算法来完成指定长度的子链表反转操作: ```javascript function reverseLinkedList(head, k) { let current = head; let prev = null; let next = null; let count = 0; while (current !== null && count < k) { next = current.next; // Store reference to the rest of the list. current.next = prev; // Reverse link direction. prev = current; // Move 'prev' one step forward. current = next; // Move 'current' one step forward. count++; } if (next !== null) { // If there's still more nodes after reversal, head.next = reverseKGroup(next, k); // Recursively call function for remaining part. } return prev || head; // Return new head node or original head if no changes made. } // Wrapper function that checks whether enough nodes exist before reversing them. function reverseKGroup(head, k) { let count = 0; let temp = head; // Check if we have at least k nodes left in the linked list. while (temp != null && count < k) { temp = temp.next; count++; } if (count === k) { return reverseLinkedList(head, k); } else { return head; } } ``` 这段代码实现了对给定链表按照每 k 个节点为单位进行反转的功能[^1]。当剩余节点不足 k 个时,则保持原样不作任何改变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值