单向链表每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值,返回逆序后的链表的头指针。

将单向链表完全翻转:

	public static ListNode reverse(ListNode start){
		ListNode cur= start;
		ListNode pre = null;
		ListNode next= null;
		while(cur!=null){
			next = cur.next;
			cur.next= pre;
			//同步向后移动
			pre = cur;
			cur = next;
		}
			return pre;
	}
每k个元素翻转一次

完整代码:

public class LinkNode {

	
	public static class ListNode {
	    int val;
	    ListNode next = null;

	    ListNode(int val) {
	        this.val = val;
	    }

		public ListNode() {
			// TODO Auto-generated constructor stub
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ListNode test = new ListNode(0);
		ListNode cur = test;
		for (int i = 1; i < 3; i++) {
			cur.next=new ListNode(i);
			cur = cur.next;
		}
		ListNode read = new ListNode();
		read=inverse(test, 3);
		while (read!=null) {
			System.out.println(read.val);
			read=read.next;
		}
	}
	public static  ListNode inverse (ListNode head,int k){
		if(k<2) return head;
		ListNode cur=head;
		ListNode pre=null;
		ListNode start = null;
		ListNode next = null;
		int count =1 ;
		while (cur!=null) {
			next= cur.next;
			if (count==3) {
				start=pre==null?head:pre.next;
				//第一次便可以确定头结点,以后头结点保持不变
				head=pre==null?cur:head;
				//将需要翻转的区间使用pre和right封闭。对区间内部的进行完全链表翻转
				//然后将头结点和尾结点与pre和right相连接,保持链表的连续性.
				reverse(pre, start, cur, cur.next);
				//翻转后的头结点将变成尾结点,作为下一次的pre
				pre=start;
				//基数器清零
				count=0;
			}
			cur=next;
			count++;
		}
		return head;
	}
	
	public static void reverse(ListNode left,ListNode start,ListNode end,ListNode right){
		ListNode cur= start;
		ListNode pre = left;
		ListNode next= null;
		while(cur!=right){
			next = cur.next;
			cur.next= pre;
			//同步向后移动
			pre = cur;
			cur = next;
		}
		if (left!=null) {
			left.next=end;
		}
		start.next=right;
	}
	
	
	

}
参考博客:

JAVA中关于链表的操作和基本算法

http://blog.csdn.net/kerryfish/article/details/24043099

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述: 给定一个链表,每k个节点为一组进行翻转。例如,给定链表1->2->3->4->5,k=2,则应返回2->1->4->3->5。请注意,你需要在不修改链表节点值的情况下完成此操作。 示例: 输入: 1->2->3->4->5, k = 2 输出: 2->1->4->3->5 思路: 这道题是链表的操作,我们可以用递归来实现,每次递归处理k个节点,翻转它们,返回翻转后的头节点,然后将它接到上一组的尾部。 具体实现: 首先,我们需要定义一个辅助函数reverse,用来翻转链表。它的实现方法是,用三个指针pre、cur、next来遍历链表,将cur的next指向pre,然后将三个指针往后移动一个节点,重复这个过程,直到遍历完整个链表。 接下来,我们可以定义一个递归函数,它的参数是链表头节点和k。如果链表的长度小于k,说明不需要翻转,直接返回头节点;否则,我们先翻转前k个节点,然后将它们的尾节点接到下一组翻转后的头节点,递归处理下一组节点。每次递归返回的是翻转后的头节点,最后将所有头节点连接起来即可。 代码实现: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverse(ListNode* head) { ListNode* pre = NULL; ListNode* cur = head; while (cur != NULL) { ListNode* next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; } ListNode* reverseKGroup(ListNode* head, int k) { ListNode* cur = head; int cnt = 0; while (cur != NULL && cnt != k) { // 找到第k+1个节点 cur = cur->next; cnt++; } if (cnt == k) { // 如果找到了,就翻转前k个节点 cur = reverseKGroup(cur, k); // 递归处理下一组节点 while (cnt-- > 0) { // 将翻转后的头节点接到下一组节点的尾部 ListNode* next = head->next; head->next = cur; cur = head; head = next; } head = cur; } return head; } };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值