💻
Software Development
DSA
DSA
  • Data structure
    • Bit
      • MSB & LSB
  • Arrays
  • Linked List
  • Stack
  • Queue
  • Hash Table
  • Tree
  • Graph
  • Algorithm
    • Bitwise
      • Challenges
    • Array
      • Two pointer
      • Sliding Window
      • Kadane Algorithm
      • Naive Pattern Searching
      • Knuth-Morris-Pratt Algorithm (KMP)
      • Quick Select Algorithm
      • Boyer - Moore Majority Vote Algorithm
      • Floyd's Cycle Detection Algorithm
    • Linked List
      • Challenges
    • Searching
      • Binary Search
        • Challenges
      • Linear Search
      • Depth First Search
      • Breadth First Search
      • Fuzzy Search
      • Rabin-Karp Algorithm
      • A* Algorithm
      • Z Algorithm
    • Sorting
      • Dutch National Flag Algorithm
    • String
      • Manacher Algorithm
    • Gready
      • Knapsack Problem Algorithms
    • Backtracking
    • Tree
      • Moris Traversal
        • Preorder
        • Inorder
    • Graph
      • Biconnected Components
        • Tarjan's Algorithm
      • Kruskal's Algorithm
      • Bellman Ford Algorithm
      • Floyd Warshall Algorithm
      • Topological Sort Algorithm
      • Flood Fill Algorithm
      • Lee Algorithm
      • Boruvka's Algorithm
      • Johnson's Algorithm
      • Kosaraju's Algorithm
    • Rate limitting
      • Token bucket
      • Leaking bucket
      • Fixed window counter
      • Sliding window log
      • Sliding window counter
    • Prioroty Queue
      • Dijkstra's Algorithm
      • Huffman Coding Compression Algorithm
      • Prim's Algorithm
      • kth largest element in an array
    • More
      • Dynamic Programming
      • Pigeonhole Principle
        • Challenges
      • Euclidean algorithm
      • Brian Kernighan’s algorithm
      • Sieve of Eratosthenes Algorithm
      • Sieve of Atkin
      • Luhn Algorithm
      • Maximizing XOR
    • Some pattern to remember when facing the challenges
Powered by GitBook
On this page
  • Merger two sort linked lists
  • Delete Node
  1. Algorithm
  2. Linked List

Challenges

PreviousLinked ListNextSearching

Last updated 19 days ago

Merger two sort linked lists

=> Pattern:

def mergeLists(head1, head2):
    # Dummy node to simplify code
    head = SinglyLinkedListNode(0)
    # Pointer to the current node in the merged list
    current = head
    
    if (head1 is None):
        return head2
    
    if (head2 is None):
        return head1
        
    # Iterate until one of the lists is exhausted
    while (head1 and head2):
        if head1.data < head2.data:
            current.next = head1
            head1 = head1.next
        else:
            current.next = head2
            head2 = head2.next
        current = current.next
        
    # Append the remaining nodes of the non-empty list
    if head1:
        current.next = head1
    else:
        current.next = head2
        
    # Return the merged list (excluding the dummy node)
    return head.next

Delete Node

public void DeleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
Merge two sorted linked lists | HackerRankHackerRank
Delete Node in a Linked List - LeetCodeLeetCode
Logo
Logo