leetCode 61.Rotate List (旋转链表) 解题思路和方法

Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:

Given 1->2->3->4->5->NULL and k = 2,

return 4->5->1->2->3->NULL.

思路:题目很清晰,思路是先得到链表长度,再从头开始直到特定点,开始变换连接即可。

代码如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(k == 0 || head == null)
            return head;
        int len = 0;
        ListNode first = head;//头结点
        ListNode last = null;//尾节点
        while(head != null){
            len++;//求长度
            last = head;//最后一个节点
            head = head.next;//循环条件
        }

        k = k%len;//如果k>len,取余数
        int n = 0;
        head = first;//标记到头结点
        while(head!= null){
            if(++n == (len - k))//判断是否到达位置
                break;
            head = head.next;
        }
        //以下为交换位置
        last.next = first;
        first = head.next;
        head.next = null;
        return first;
    }
}/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(k == 0 || head == null)
            return head;
        int len = 0;
        ListNode first = head;//头结点
        ListNode last = null;//尾节点
        while(head != null){
            len++;//求长度
            last = head;//最后一个节点
            head = head.next;//循环条件
        }

        k = k%len;//如果k>len,取余数
        int n = 0;
        head = first;//标记到头结点
        while(head!= null){
            if(++n == (len - k))//判断是否到达位置
                break;
            head = head.next;
        }
        //以下为交换位置
        last.next = first;
        first = head.next;
        head.next = null;
        return first;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 18:35:04

leetCode 61.Rotate List (旋转链表) 解题思路和方法的相关文章

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

leetCode 86.Partition List(分区链表) 解题思路和方法

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example, Given 1->4->3->

leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解:但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码. 代码一(超时未过): /** * Definition for singly-link

leetCode 25.Reverse Nodes in k-Group (以k个节点为一组反转链表) 解题思路和方法

Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and return its modified 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 valu

[leetcode]61. Rotate List反转链表k个节点

类似于找链表的后k个节点 不同的是要把前边的接到后边 public ListNode rotateRight(ListNode head, int k) { //特殊情况 if (head==null||head.next==null||k==0) return head; int len = 0; ListNode p = head; //计算链表长度,防止k大于长度 while (p!=null) { len++; p = p.next; } //k大于等于len的情况 k = k>=len

leetCode 27.Remove Element (删除元素) 解题思路和方法

Remove Element Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路:此题和26题一脉相承,算法上不难,具体如代码所示: public class

leetCode 51.N-Queens (n皇后问题) 解题思路和方法

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur

leetCode 57.Insert Interval (插入区间) 解题思路和方法

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], i