1. Description
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
2. Example
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
3. Code
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="C" active="active"]
struct ListNode * reverseKNode(struct ListNode * start, int k) { struct ListNode * end = start->next; int i = k; while(i-- > 0 && end != NULL ) { end = end->next; } if(i >= 0) { return end; } struct ListNode *current = start->next-> next, *preNode = start-> next; while(--k > 0) { struct ListNode *tmp = current->next; current->next = preNode; preNode = current; current = tmp; } end = start->next; end->next = current; start->next = preNode; return end; } struct ListNode * reverseKGroup(struct ListNode* head, int k) { if(head == NULL || k == 1 ) { return head; } struct ListNode * dummy = (struct ListNode *) malloc(sizeof(struct ListNode)); dummy->next = head; struct ListNode * start = dummy; while(start != NULL) { start = reverseKNode(start, k); } return dummy->next; }
[/restab]
[restab title="Java"]
public ListNode reverseKGroup(ListNode head, int k) { if(k == 1 || head == null){ return head; } ListNode dummy = new ListNode(0); dummy.next = head; ListNode start = dummy; while(start != null){ start = reverseKNode(start, k); } return dummy.next; } private ListNode reverseKNode(ListNode start, int k){ ListNode end = start; int i = k; while(i-- > 0 && end.next != null){ end = end.next; } if(i >= 0){ return null; } ListNode current = start.next.next, preNode = start.next; while(--k > 0){ ListNode tmp = current.next; current.next = preNode; preNode = current; current = tmp; } end = start.next; end.next = current; start.next = preNode; return end; }
[/restab]
[/restabs]
4.Test
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="C" active="active"]
#include#include struct ListNode { int val; struct ListNode * next; }; struct ListNode * reverseKNode(struct ListNode * start, int k) { struct ListNode * end = start->next; int i = k; while(i-- > 0 && end != NULL ) { end = end->next; } if(i >= 0) { return end; } struct ListNode *current = start->next-> next, *preNode = start-> next; while(--k > 0) { struct ListNode *tmp = current->next; current->next = preNode; preNode = current; current = tmp; } end = start->next; end->next = current; start->next = preNode; return end; } struct ListNode * reverseKGroup(struct ListNode* head, int k) { if(head == NULL || k == 1 ) { return head; } struct ListNode * dummy = (struct ListNode *) malloc(sizeof(struct ListNode)); dummy->next = head; struct ListNode * start = dummy; while(start != NULL) { start = reverseKNode(start, k); } return dummy->next; } int main() { struct ListNode * ln = (struct ListNode *) malloc(3 *sizeof(struct ListNode)); ln->val = 1; (ln + 1)->val = 2; (ln + 2)->val = 3; ln->next = ln + 1; (ln + 1)->next = ln + 2; (ln + 2)->next = NULL; struct ListNode * result = reverseKGroup(ln, 3); while (result != NULL) { printf("%d", result->val); result = result->next; } system("pause"); return 0; }
[/restab]
[restab title="java"]
import org.junit.Test; class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public class LeetCode0025 { public ListNode reverseKGroup(ListNode head, int k) { if(k == 1 || head == null){ return head; } ListNode dummy = new ListNode(0); dummy.next = head; ListNode start = dummy; while(start != null){ start = reverseKNode(start, k); } return dummy.next; } private ListNode reverseKNode(ListNode start, int k){ ListNode end = start; int i = k; while(i-- > 0 && end.next != null){ end = end.next; } if(i >= 0){ return null; } ListNode current = start.next.next, preNode = start.next; while(--k > 0){ ListNode tmp = current.next; current.next = preNode; preNode = current; current = tmp; } end = start.next; end.next = current; start.next = preNode; return end; } @Test public void test(){ ListNode ln1 = new ListNode(1); ListNode ln2 = new ListNode(2); ListNode ln3 = new ListNode(3); ln1.next = ln2; ln2.next = ln3; ListNode result = reverseKGroup(ln1, 3); while(result != null){ System.out.print(result.val); result = result.next; } } }
[/restab]
[/restabs]
Comments | NOTHING