码迷,mamicode.com
首页 > 编程语言 > 详细

链表算法总结

时间:2016-03-14 21:59:14      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

这篇博客就总结一下关于链表的各种题,来源POJ和LeetCode,一点一点的扩充,遇到题目就补进来。

(1)LeetCode  61. Rotate List

双指针,先计数求长度,不要一前一后一起跑

public class Solution {
    public ListNode rotateRight(ListNode head, int n) {
        if (head==null||head.next==null) return head;
        ListNode dummy=new ListNode(0);
        dummy.next=head;
        ListNode fast=dummy,slow=dummy;
    
        int i;
        for (i=0;fast.next!=null;i++)//Get the total length 
            fast=fast.next;
    
        for (int j=i-n%i;j>0;j--) //Get the i-n%i th node
            slow=slow.next;
    
        fast.next=dummy.next; //Do the rotation
        dummy.next=slow.next;
        slow.next=null;
    
        return dummy.next;
    }
}

 

链表算法总结

标签:

原文地址:http://www.cnblogs.com/yueyanglou/p/5277143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!