Reverse Linked List I

Reverse a linked list.

Example

For linked list 1->2->3, the reversed linked list is 3->2->1

分析:

典型的3 pointers 问题。

 /**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The head of linked list.
* @return: The new head of reversed linked list.
*/
public ListNode reverse(ListNode head) {
if (head == null) return head;
ListNode prev = null;
ListNode cur = head;
ListNode next = null;
while (cur != null) {
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
}

Reverse Linked List II

Reverse a linked list from position m to n.

Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.

Example

Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL.

分析:

我们需要把list分成三段。首先得到第一段的最后一个node,然后reverse中间部分。最后把三个部分链接起来。

 /**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @oaram m and n
* @return: The head of the reversed ListNode
*/
public ListNode reverseBetween(ListNode head, int m , int n) {
if (m == n) return head;
ListNode tempHead = new ListNode();
tempHead.next = head; // reach the end of the first part
ListNode partEnd = tempHead;
for(int k = ; k < m; k++) {
partEnd = partEnd.next;
}
// save it later to connect to the last part.
ListNode secondPartTail = partEnd.next; // reverse the middle part
ListNode prev = null;
ListNode current = partEnd.next;
ListNode next = null; for (int p = ; p <= n - m + ; p++) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
// connect three parts
partEnd.next = prev;
secondPartTail.next = current; return tempHead.next;
}
}

转载请注明出处:cnblogs.com/beiyeqingteng/

Reverse Linked List | & ||的更多相关文章

  1. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  2. [LeetCode] Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  3. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

  4. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  5. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  6. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

  7. 14. Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  8. 【12_206】Reverse Linked List

    本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...

  9. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

随机推荐

  1. poppin_xpower_ 常城

  2. keep_on _coding——js_good_parts

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. javascript 规范

    关于变量及方法等的命名,没有硬性规定,但是为了规范,遵循一些约定还是有必要的. 变量定义: 用var 关键字将要使用的变量定义在代码开头,变量间用分号隔开. 原因有二: 一是便于理解,知道下面的代码会 ...

  4. Java-ArrayList和Vector的区别

    这两个类都实现了List接口(List接口继承了Collection接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种动态的数组,我们以后可以按位置索引号取出某个元素, ...

  5. iOS-编译简单静态库初探

    首先声明,我写的这些网上都有更详细的内容,在这里只是写下我自己总结的一些重要内容,具体步骤如下: 事先准备:新建工程-Framework & Library - Cocoa Touch Sta ...

  6. BZOJ2115 [Wc2011] Xor

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

  7. POJ3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44720   Accepted: 20995 ...

  8. ubuntu设置开机启动图形应用程序,替换默认图形桌面

    直接将启动程序放在rc.local即可.但是如果自动启动的程序奔溃后,会返回到ubuntu的unity桌面系统. 我遇到的问题是程序还有调用 xset 去定时关闭屏幕.在桌面启动后调用没问题.如果rc ...

  9. 网络html查看器

    1)演示效果:

  10. 用多态来实现U盘,Mp3,移动硬盘和电脑的对接,读取写入数据。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...