输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 输出:Reference of the node with value = 8 输入解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
示例 2:
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4],skipA = 3, skipB = 1 输出:Reference of the node with value = 2 输入解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
classSolution: defremoveNthFromEnd(self, head: ListNode, n: int) -> ListNode: length = 0 l1 = head while l1 isnotNone: length += 1 l1 = l1.next if length == 1: returnNone if n == 1: l1 = head while l1.next.next isnotNone: l1 = l1.next l1.next = None return head if n == length: head = head.next return head length1 = length - n -1 a = 0 l1 = head while a < length1: l1 = l1.next a += 1 print(a,l1.val) #print(a) #print(l1.val) l1.next = l1.next.next return head
class Solution: def swapPairs(self, head: ListNode) -> ListNode: if head is None or head.next is None: return head next = head.next head.next = self.swapPairs(next.next) next.next = head return next
classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defisPalindrome(self, head: ListNode) -> bool: # O(N) l1 = head list = [] while l1 isnotNone: list.append(l1.val) l1 = l1.next a = 0 b = len(list) - 1 while a < b: if list[a] == list[b]: a += 1 b -= 1 else: break if a >= b: returnTrue else: returnFalse
# Definition for singly-linked list. classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defisPalindrome(self, head: ListNode) -> bool: slow, fast = head, head while fast isnotNoneand fast.next isnotNone: slow = slow.next fast = fast.next.next if fast isnotNone: slow = slow.next newhead = slow newhead = self.reverse(newhead) while head isnotNoneand newhead isnotNone: if head.val != newhead.val: returnFalse head = head.next newhead = newhead.next returnTrue
defreverse(self, head): prev = None current = head while current: next_node = current.next current.next = prev prev = current current = next_node
public ListNode[] splitListToParts(ListNode root, int k) { int N = 0; ListNode cur = root; while (cur != null) { N++; cur = cur.next; } int mod = N % k; int size = N / k; ListNode[] ret = new ListNode[k]; cur = root; for (int i = 0; cur != null && i < k; i++) { ret[i] = cur; int curSize = size + (mod-- > 0 ? 1 : 0); for (int j = 0; j < curSize - 1; j++) { cur = cur.next; } ListNode next = cur.next; cur.next = null; cur = next; } return ret; }
# Definition for singly-linked list. classListNode: def__init__(self, val=0, next=None): self.val = val self.next = next
classSolution: defoddEvenList(self, head: ListNode) -> ListNode: if head isNoneor head.next isNone: return head odd, even = head, head.next evenHead = even while even isnotNoneand even.next isnotNone: odd.next = odd.next.next odd = odd.next even.next = even.next.next even = even.next odd.next = evenHead return head
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public ListNode oddEvenList(ListNode head){ if (head == null) { return head; } ListNode odd = head, even = head.next, evenHead = even; while (even != null && even.next != null) { odd.next = odd.next.next; odd = odd.next; even.next = even.next.next; even = even.next; } odd.next = evenHead; return head; }
# Definition for singly-linked list. classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defhasCycle(self, head: ListNode) -> bool: if head isNone: returnFalse if head.next isNone: returnFalse low,fast = head,head.next while fast isnotNoneand low isnotNoneand fast.next isnotNone: if fast == low: returnTrue else: fast = fast.next.next low = low.next returnFalse