Jan 10 - Reverse Linked List;Data Structure; Linked List; Pointer; Iteration & Recursion

Iteration:

代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) return null;
ListNode tail = head.next;
ListNode cur = head;
while(tail!= null){
ListNode nex = tail.next;
tail.next = head;
cur.next = nex;
head = tail;
tail = nex;
}
return head;
}
}

Recursion:

代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode cur = reverseList(head.next);
head.next.next = head;
head.next = null;
return cur;

}

}

时间: 2024-08-14 06:34:33

Jan 10 - Reverse Linked List;Data Structure; Linked List; Pointer; Iteration & Recursion的相关文章

Jan 12 - Delete Node in a Linked List; Data Structure; Linked List; Pointer;

代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public void deleteNode(ListNode node) { if(node == null) return; while(node.next != null)

Swap Nodes in Pairs; Data Structure; Linked List; Pointer;

注意用指针记录节点的前一个节点位置. 代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode swapPairs(ListNode head) { if(head == null || head.nex

Jan 10 - Isomorphic Strings; String; Data Type: char; digit and letter

创建两个数组 分别记录两个字符串 各个位置字符出现的上一个位置 通过比较当前字符的上一个位置是否相同 判断是否同构 比较坑爹的是 一开始以为只有字母 所以纠结于怎么找出字母来... 代码如下: public class IsomorphicStrings { public static boolean isIsomorphic(String s, String t) { int[] alpha1 = new int[1000]; int[] alpha2 = new int[1000]; for

Leetcode: All O`one Data Structure

Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string. Dec(Key) - If Key's value is 1, remove it from the data structu

Use the Right Algorithm and Data Structure

Use the Right Algorithm and Data Structure Jan Christiaan "JC" van Winkel A big bank with many branch offices complained that the new computers it had bought for the tellers were too slow. This was in the time before everyone used electronic ban

hdu-5929 Basic Data Structure(双端队列+模拟)

题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 207    Accepted Submission(s): 41 Problem Description Mr. Frog learned a basic data structure recently, which is called

HDU 5929 Basic Data Structure 模拟

Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack: ? PUSH x: p

[转]Data Structure Recovery using PIN and PyGraphviz

Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html -------------------------------- Data Structure Recovery using PIN and PyGraphviz ? This is a simple POC PIN tool to recover data structures in dynamically lin

HDU 5929 Basic Data Structure

题目:Basic Data Structure 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:t个测试数据,每个数据给出一个m表示操作数量,操作分为4种:PUSH x(x:0或1),POP,REVERSE(翻转栈里面的数),QUERY(假使栈内的数一个个出栈,并按出栈顺序将他们与非起来,问最终结果(注意,并不是真的出栈)) 思路: 做题的时候忘记了与非是按出栈顺序来的,一直按栈底到栈顶的顺序来,WA到死... 根据与非的性质,1.0与非