算法--链表的K逆序问题

转载请标明出处http://www.cnblogs.com/haozhengfei/p/9e6f4dda3138cf9fab17f996ec85b624.html


链表的K逆序问题

链表的k逆序

第7节 链表的k逆序练习题

有一个单链表,请设计一个算法,使得每K个节点之间逆序,如果最后不够K个节点一组,则不调整最后几个节点。例如链表1->2->3->4->5->6->7->8->null,K=3这个例子。调整后为,3->2->1->6->5->4->7->8->null。因为K==3,所以每三个节点之间逆序,但其中的7,8不调整,因为只有两个节点不够一组。

给定一个单链表的头指针head,同时给定K值,返回逆序后的链表的头指针。

Java (javac 1.7)

代码自动补全

1

import java.util.*;

2


3

/*

4

public class ListNode {

5

    int val;

6

    ListNode next = null;

7


8

    ListNode(int val) {

9

        this.val = val;

10

    }

11

}*/

12

public class KInverse {

13

    public ListNode inverse(ListNode head, int k) {

14

        if (k < 2) {

15

            return head;

16

        }

17

        ListNode cur = head;//正在遍历的当前节点

18

        ListNode start = null;

19

        ListNode pre = null;

20

        ListNode next = null;//待遍历的下一个节点

21

        int count = 1;

22

        while (cur != null) {

23

            next = cur.next;

24

            if (count == k) {//count满足要求开始进行逆序

25

                start = pre == null ? head : pre.next;

26

                head = pre == null ? cur : head;

27

                resign(pre, start, cur, next);

28

                pre = start;

29

                count = 0;

30

            }

31

            count++;

32

            cur = next;

33

        }

34

        return head;

35

    }

36

    public void resign(ListNode left, ListNode start, ListNode end,

37

            ListNode right) {

38

        ListNode pre = start;

39

        ListNode cur = start.next;

40

        ListNode next = null;

41

        while (cur != right) {

42

            next = cur.next;

43

            cur.next = pre;

44

            pre = cur;

45

            cur = next;

46

        }

47

        if (left != null) {

48

            left.next = end;

49

        }

50

        start.next = right;

51

    }

52

}

您的代码已保存
答案正确:恭喜!您提交的程序通过了所有的测试用例

运行

时间: 2024-08-10 02:11:42

算法--链表的K逆序问题的相关文章

数据结构实验之链表二:逆序建立链表

数据结构实验之链表二:逆序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入整数个数N,再输入N个整数,按照这些整数输入的相反顺序建立单链表,并依次遍历输出单链表的数据. Input 第一行输入整数N;:第二行依次输入N个整数,逆序建立单链表. Output 依次输出单链表所存放的数据. Sample Input 10 11 3 5 27 9 12 43 16 84 22 Sample Output 22

算法题:链表的递归逆序

#include <iostream> using namespace std; struct Node { int data; Node *next; Node(int d = int()) :data(d), next(NULL){} }; class Clist { public: Clist(int a[], int n) :first(NULL) { int i = 0; Node *p = NULL; for (; i < n; i++) { Node *s = new No

(单链表)单链表的整体逆序和局部逆序

题目一:将单链表翻转. 思路:有三种方式. 一:用数组存储单链表的值,然后重新逆序赋值,效率较低. 二:利用三个指针,在原来的基础上进行逆序.这种方法比较实用,效率也高. 三:从第2个节点到第N个节点,依次逐节点插入到第1个节点(head节点)之后,最后将第一个节点挪到新表的表尾.需要新建一个链表,这种方法和第二种差不多. 这里我就写出第二种方法,比较实用. 代码(方法二): struct ListNode { int val; ListNode *next; ListNode(int x) :

链表元素的逆序输出

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define ElemType int 5 #define N 5 //定义链表的结点数目 6 typedef struct Node 7 { 8 ElemType data; 9 struct Node *next; 10 }Node; 11 typedef struct Node *LinkList; 12 13 14 15 Node* CreatList(Node *La,int

2117=数据结构实验之链表二:逆序建立链表

1 #include <stdio.h> 2 #include <stdlib.h> 3 struct node 4 { 5 int data; 6 struct node*next; 7 }; 8 int main() 9 { 10 int n,i; 11 struct node*head,*p; 12 head=(struct node*)malloc(sizeof(struct node));//为head在这个链表中开辟一个空间. 13 head->next=NULL

[LeetCode]88. Swap Nodes in Pairs链表成对逆序

Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, onl

[LeetCode]2. Add Two Numbers用链表逆序存储的两个数相加

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

数据结构之 线性表 逆序简历链表

数据结构实验之链表二:逆序建立链表 Time Limit: 1000MS Memory limit: 65536K 题目描述 输入整数个数N,再输入N个整数,按照这些整数输入的相反顺序建立单链表,并依次遍历输出单链表的数据. 输入 第一行输入整数N;: 第二行依次输入N个整数,逆序建立单链表. 输出 依次输出单链表所存放的数据. 示例输入 10 11 3 5 27 9 12 43 16 84 22 示例输出 22 84 16 43 12 9 27 5 3 11 比顺序创建链表还要简单! #inc

单链表 - 神一般的逆序

在这篇文章 Linked List 中学习了如何写出单链表的基本操作. 现在来写一个程序进行单链表的逆序操作: void Reversal( List L ) { Position pReversal; Position P,Pnext; P = L->Next; pReversal->Next = NULL; while( P != NULL ) { Pnext = P->Next; P->Next = pReversal->Next; pReversal->Next