Leetcode 25. k a group of flip linked lists (reverse nodes in K-group)

Source: Internet
Author: User
Description

 

Give a linked list, eachKNodes in a group, and return the flipped linked list.

KIs a positive integer whose value is smaller than or equal to the length of the linked list. If the total number of nodes is notKTo keep the remaining nodes in the original order.

Example:

Given the linked list:1->2->3->4->5

WhenK= 2, should return:2->1->4->3->5

WhenKWhen it is 3, it should return:3->2->1->4->5

Note:

  • Your algorithm can only use extra space of constants.
  • Instead of simply changing the internal value of a node, You need to actually exchange nodes.

 

Solutions

 

First, traverse the entire linked list to get the total length, and then reverse each K nodes in turn. Each time you save the first and last nodes after the current node is reversed, you can splice the linked list of up and down K nodes; record the first node of the new linked list after the first reversal. If the last node is less than K, splice the End Node of the last reverse linked list to the subsequent node.

 

Code

 

 1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     ListNode *next; 6  *     ListNode(int x) : val(x), next(NULL) {} 7  * }; 8  */ 9 class Solution {10 public:11     ListNode* reverseKGroup(ListNode* head, int k) {12         ListNode *left = head, *right = head, *newHead = NULL;13         int len = 0;14         while(right){15             len++;16             right = right->next;17         }18         if(len < k) return head;19         right = head;20         while(len >= k){21             ListNode *node = right;22             ListNode *h = right;23             right = right->next;24             for(int i = 0; i < k-1; i++){25                 ListNode *next = right->next;26                 right->next = node;27                 node = right;28                 right = next;29             }30             if(newHead == NULL) newHead = node;31             else{32                 left->next = node;33                 left = h;34             }35             len -= k;36         }37         left->next = right;38         return newHead;39     }40 };

 

Leetcode 25. k a group of flip linked lists (reverse nodes in K-group)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.