linked

LeetCode_237. Delete Node in a Linked List

心已入冬 提交于 2019-12-02 18:31:02
237. Delete Node in a Linked List Easy Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list -- head = [4,5,1,9], which looks like following: Example 1: Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function. Example 2: Input: head = [4,5,1,9], node = 1 Output: [4,5,9] Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function. Note:

ArrayList和LinkedList介绍

守給你的承諾、 提交于 2019-12-02 16:49:11
java.util.ArrayList集合的数据存储结构是数组,且是多线程,元素增删慢,查找快, 由于日常使用开发大多数为查询数据,遍历数据,所以ArrayList是最常用的集合。上一节已写了。 java.util.LinkedList集合属于双向链表,查询慢,增删快,有大量操作首尾元素的方法,下面用代码实现LinkedList的方法: package 集合; import java.util.LinkedList; /* LinkedList集合的特点: * 1.底层是一个链表形式的结构,查询慢,增删快 * 2.里面包含了大量操作首尾元素的方法 * 注意:使用LinkedList集合特有的方法,不能使用多态 */ public class demo02LinkedList { public static void main(String[] args) { show1(); System.out.println("------------------"); show2(); } private static void show1() { LinkedList<String> linked= new LinkedList<String>(); //添加元素 linked.add("a"); linked.add("b"); linked.add("c"); System.out

LeetCode_234. Palindrome Linked List

强颜欢笑 提交于 2019-12-02 09:53:15
234. Palindrome Linked List Easy Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true Follow up: Could you do it in O(n) time and O(1) space? package leetcode.easy; public class PalindromeLinkedList { public boolean isPalindrome(ListNode head) { if (head == null) { return true; } java.util.List<Integer> list = new java.util.ArrayList<>(); while (head != null) { list.add(head.val); head = head.next; } int end = list.size() - 1; for (int i = 0; i < end; i++, end--) { if (!list.get(i).equals(list.get(end))) {

[LeetCode] 876. Middle of the Linked List

蓝咒 提交于 2019-12-01 01:39:16
Easy Given a non-empty, singly linked list with head node head , return a middle node of linked list. If there are two middle nodes, return the second middle node. Example 1: Input: [1,2,3,4,5] Output: Node 3 from this list (Serialization: [3,4,5]) The returned node has value 3. (The judge's serialization of this node is [3,4,5]). Note that we returned a ListNode object ans, such that: ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL. Example 2: Input: [1,2,3,4,5,6] Output: Node 4 from this list (Serialization: [4,5,6]) Since the list has two middle nodes

[LC] 203. Remove Linked List Elements

…衆ロ難τιáo~ 提交于 2019-11-30 12:06:24
Remove all elements from a linked list of integers that have value val . Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeElements(self, head: ListNode, val: int) -> ListNode: dummy = ListNode(-1) dummy.next = head cur = dummy while cur.next is not None: if cur.next.val != val: cur = cur.next else: cur.next = cur.next.next return dummy.next 来源: https://www.cnblogs.com/xuanlu/p/11583638.html

Leetcode 206 Reverse Linked List

南楼画角 提交于 2019-11-29 22:19:59
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def reverseList(self, head: ListNode) -> ListNode: if head == None: return None # L M R # M -> L L = None M = None R = head while R.next != None: L = M M = R R = R.next M.next = L R.next = M return R 来源: https://blog.csdn.net/huyun9666/article/details/100941222

LeetCode:92. Reverse Linked List II

眉间皱痕 提交于 2019-11-29 21:49:13
一、问题介绍 Reverse a linked list from position m to n . Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 二、思路解析 对于链表的问题,需要建一个preHead,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过preHead->next来获得新链表的头结点。 三、代码实现 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode *dummy = new ListNode(0), *pre = dummy; dummy->next = head; for (int i = 1; i <= m - 1; ++i) pre

1. Duplicate a linked list

半世苍凉 提交于 2019-11-29 16:04:58
题目 Write a function duplicate(head) to duplicate the linked list. Here head is the id of the head node of a linked list. Example: For linked list 0 -> 1 -> 2, modify the linked list (in-place) into 0 -> 1 -> 2 -> 0 -> 1 -> 2. 代码 class ListNode: def __init__(self,data): self.next=None self.data=data def duplicate(head): if head==None: return None temp = newhead = ListNode(0) head1 = head while head1 != None: temp.data = head1.data head1 = head1.next temp.next = ListNode(0) temp = temp.next temp.next = head.next #magic step... return newhead def printlistnode(head): while head!=None: print(head

LeetCode 234. Palindrome Linked List

陌路散爱 提交于 2019-11-29 14:00:29
题目 : Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true Follow up: Could you do it in O(n) time and O(1) space? 这道题是要求一个给定的链表是否为回文列表(然后满脑子513orz),刚开始想到的办法就是reverse整个链表然后判断是否相等,但是这样会使用O(n)的空间,看了discussion发现可以直接reverse后半部分链表,让快慢指针分别走两步和走一步,找到链表的中间值,再把慢指针后面的给反转一下,然后代码就很快写出来了。在reverse的时候一不小心还return错了以后一定要注意,写isPal的时候注意中间处理的细节,其他好像就没什么了。时间复杂度O(n),24ms,67.11%,空间复杂度O(1),12.9M,43.1%(虽然看到有人在讨论这把原始输入给改掉了行不行但是我觉得不影响)。 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode

160. Intersection of Two Linked Lists

折月煮酒 提交于 2019-11-27 14:08:29
easy https://leetcode.com/problems/intersection-of-two-linked-lists/ 题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. Notes: If the two linked lists have no intersection at all, return null . The linked lists must retain their original structure after the function returns. You may assume there are no cycles anywhere in the entire linked structure. Your code should preferably run in O(n) time and use only O(1) memory. 方法一:分别遍历 A 和 B 链表得到长度,较长的那个先走长度差值的 node 数,然后 A、B 一起走